1

I have a table view which loads custom table view cells. In the table view cell I have an image view, among other controls. The problem is that the table view stutters while scrolling, even though the images are being read from Cache or documents directory (using either isn't making any difference)

Here is the code for displaying the table view cell:

func tableView(tableView: UITableView,
               willDisplayCell cell: UITableViewCell,
                               forRowAtIndexPath indexPath: NSIndexPath) {

if cell.isKindOfClass(EventsTableViewCell) {

    let eventCell = cell as! EventsTableViewCell

    let list = self.eventsList

    if list.count > 0 {

        let event = list.objectAtIndex(indexPath.row) as! Event


//other cell labels configured here

        //imageCache is NSCache instance
        if let imageData = self.imageCache.objectForKey(event.name) as? NSData {

            let image = UIImage(data: imageData)

            eventCell.eventImageView.image = image
        }
        else if event.imageUrl != "" {

            eventCell.setImageToCell(event)
        }


    }

}

}

func tableView(tableView: UITableView,
               cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cellIdentifier = "eventCell"

var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)

if cell == nil {

    let nibCollection = NSBundle.mainBundle().loadNibNamed("EventsTableViewCell",
                                                           owner: nil,
                                                           options: nil)

    cell = (nibCollection as NSArray).objectAtIndex(0) as? EventsTableViewCell
}

cell?.selectionStyle = .None

return cell!
}

In EventsTableViewCell,

func setImageToCell(event: Event) {

        //apply placeholder image. This image will be replaced if an image is found for the particular event.

        if event.image != nil {

            dispatch_async(dispatch_get_main_queue(), {

                    self.eventImageView.image = event.image
            })
        }
}

So even though none of the images are being downloaded here, the table view still can't handle the scrolling properly. I am not sure what I am doing wrong. Can the size of the images be an issue here?

StudentX
  • 2,506
  • 5
  • 19
  • 28
  • What size are the images? – skymook Sep 17 '16 at 09:28
  • They vary in size.. they range from around 100 KB to 2 MB – StudentX Sep 17 '16 at 09:50
  • 2MB!! is quite a large size, you might consider to lower the size of the images. But, I think your issue is somewhere else. Might be setting the images in your cell in cellForRowAtIndexPath help you. You can also try moving your image reading from cache into dispatch_async(dispatch_get_main_queue(), { }) and see if it works. – Sanchit Kumar Singh Sep 17 '16 at 10:10
  • @SanchitKumarSingh I noticed a very minor improvement after following your approach – StudentX Sep 17 '16 at 10:26
  • Try to print time stamps from in the start and end of cellForRowAtIndexpath, also check the time profiler for the cell. In time profiler you will see which cell binding time is more. – Sanchit Kumar Singh Sep 17 '16 at 16:52

0 Answers0