简体   繁体   中英

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.

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.

What i did was remove the image from imageView and set image again. It works for me.

cell.imageView.image = nil
cell.imageView.image = UIImage(named:"Calendar")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM