Alexey  Milahin Alexey Milahin - 7 months ago 52
Swift Question

TableView lag when scrolling

When you scroll through the table appear lags. Pictures, or long texts in the database do not. Sorry, for my bad English

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("audioCell", forIndexPath: indexPath) as? AudiosTableViewCell

if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "audioCell") as? AudiosTableViewCell
} else {
let realm = try! Realm()
let audios = realm.objects(Music)[indexPath.row]
let duration = audios.duration
var durationString = ""

if duration/60 < 10 {
durationString = durationString + "0" }
durationString = durationString + String(duration/60) + ":"
if duration%60 < 10 {
durationString = durationString + "0" }
durationString = durationString + String(duration%60)

cell!.artistLabel.text = audios.artist
cell!.titleLabel.text = audios.title
cell!.durationLabel.text = durationString
}
return cell!
}


If you need additional information, write exactly what you need. I've reviewed a lot of information, tried many methods, the third day I am suffering, it does not work

Answer

This

    let realm = try! Realm()

should be done on videoDidLoad or similar but only once I suggest

if audios.count == 0 {
        let realm = try! Realm()
        let audios = realm.objects(Music)[indexPath.row]
}

then replace

    let realm = try! Realm()
    let audiosStore = realm.objects(Music)

with

let audios = audiosStore[indexPath.row]

When you call

    let realm = try! Realm()

You are asking for all the objects from Realm each time.

Comments