UITableViewCell Images Changing after Scrolling Up and Down but TextLabel Text Remain Same (iOS8, Swift)

I'm trying to create an RSS app for practice. So my tableView cell has both textLabel and image. My textLabel's text and image data come from a dictionary stored in Core Data. Some cells have images, and some don't. The initial load of tableView looks fine to me. But when I scroll down and up, cell's images seem to change. The cells' textLabel text doesn't change though.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    self.configureCell(cell, atIndexPath: indexPath)
    return cell
}

func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
    let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    cell.textLabel!.text = object.valueForKey("title")!.description

    var image: UIImage?
    if let imageData = object.valueForKey("imageData") as? NSData {
        image = UIImage(data: imageData)
        let itemSize = CGSizeMake(80, 80)
        UIGraphicsBeginImageContext(itemSize)
        let imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height)
        image?.drawInRect(imageRect)
        cell.imageView?.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
}

If the initial tableView loads correctly, that means my Core Data stores my data's mapping correctly. But when scrolling down and up, configureCell is being called again since it needs to redraw the cell. cell.textLabel!.text = object.valueForKey("title")!.description is set correctly again, but not image here. Don't know why it behaves like this. Please give some pointer.

Answers


I had this same problem once and I think it has something to do with the reused cell not being in the default state so sometimes the image you set is being reused. To fix it, I just did an else condition on imageData and set the image to a default image if no image was found. I'm sure you could set it to nil here as well.

    if let image = UIImage(contentsOfFile: imagePath) {
        cell.imageView.image = image
    } else {
        // Default image or nil
        cell.imageView.image = UIImage(named: "Calendar")
    }

And I wouldn't suggest storing images as raw data in core data, as this can be very inefficient. Instead, download them to your documents directory and store a file name in core data.


Need Your Help

How do I make this expand/collapse FAQ list to work?

javascript jquery css html-lists accordion

Here's my list that I want to expand/collapse: My goal is simply to toggle expand/collapse, to have this list act like an accordion. What I don't understand is how to get the <div> from a hid...

Deployment Rails app for Docker

ruby-on-rails docker

Read many resources but still confused about Docker from deployment point of view. Trying to find out the best practices for Rails app within Docker environment, particularly interested how to solv...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.