Alexey  Milahin
Alexey Milahin

Reputation: 59

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

Upvotes: 0

Views: 232

Answers (3)

Alexander Doloz
Alexander Doloz

Reputation: 4188

tableView.dequeueReusableCellWithIdentifier("audioCell", forIndexPath: indexPath) never returns nil. If you already know cell will have class AudiosTableViewCell you can rewrite code as follows:

// Make realm property of your view controller
    let realm: Realm!

// In viewDidLoad
    realm = try! Realm()


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

    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
}

Upvotes: 0

Sean Lintern
Sean Lintern

Reputation: 3141

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.

Upvotes: 2

turushan
turushan

Reputation: 720

Can you move this code to out of cellForRow, and be sure call it before cellForRow triggers.

let realm = try! Realm()

Upvotes: 0

Related Questions