上下滚动后 UITableViewCell 图像发生变化,但 TextLabel 文本保持不变 (iOS8, Swift)
UITableViewCell Images Changing after Scrolling Up and Down but TextLabel Text Remain Same (iOS8, Swift)
我正在尝试创建一个 RSS 应用程序来练习。所以我的 tableView 单元格同时具有 textLabel 和图像。我的 textLabel 的文本和图像数据来自存储在 Core Data 中的字典。有些细胞有图像,有些则没有。 tableView 的初始加载对我来说看起来不错。但是当我上下滚动时,单元格的图像似乎发生了变化。单元格的 textLabel 文本没有改变。
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()
}
}
如果初始 tableView 加载正确,则意味着我的核心数据正确存储了我的数据映射。但是当向下和向上滚动时,configureCell
再次被调用,因为它需要重绘单元格。 cell.textLabel!.text = object.valueForKey("title")!.description
再次正确设置,但不是这里的图像。不知道为什么它会这样。请指点一下。
我曾经遇到过同样的问题,我认为这与重用的单元格未处于默认状态有关,因此有时您设置的图像会被重用。为了修复它,我只是对 imageData 做了一个 else
条件,如果没有找到图像,则将图像设置为默认图像。我相信你也可以在这里将它设置为 nil。
if let image = UIImage(contentsOfFile: imagePath) {
cell.imageView.image = image
} else {
// Default image or nil
cell.imageView.image = UIImage(named: "Calendar")
}
而且我不建议将图像作为原始数据存储在核心数据中,因为这样效率很低。相反,将它们下载到您的文档目录并将文件名存储在核心数据中。
我所做的是从 imageView 中删除图像并重新设置图像。对我有用。
cell.imageView.image = nil
cell.imageView.image = UIImage(named:"Calendar")
我正在尝试创建一个 RSS 应用程序来练习。所以我的 tableView 单元格同时具有 textLabel 和图像。我的 textLabel 的文本和图像数据来自存储在 Core Data 中的字典。有些细胞有图像,有些则没有。 tableView 的初始加载对我来说看起来不错。但是当我上下滚动时,单元格的图像似乎发生了变化。单元格的 textLabel 文本没有改变。
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()
}
}
如果初始 tableView 加载正确,则意味着我的核心数据正确存储了我的数据映射。但是当向下和向上滚动时,configureCell
再次被调用,因为它需要重绘单元格。 cell.textLabel!.text = object.valueForKey("title")!.description
再次正确设置,但不是这里的图像。不知道为什么它会这样。请指点一下。
我曾经遇到过同样的问题,我认为这与重用的单元格未处于默认状态有关,因此有时您设置的图像会被重用。为了修复它,我只是对 imageData 做了一个 else
条件,如果没有找到图像,则将图像设置为默认图像。我相信你也可以在这里将它设置为 nil。
if let image = UIImage(contentsOfFile: imagePath) {
cell.imageView.image = image
} else {
// Default image or nil
cell.imageView.image = UIImage(named: "Calendar")
}
而且我不建议将图像作为原始数据存储在核心数据中,因为这样效率很低。相反,将它们下载到您的文档目录并将文件名存储在核心数据中。
我所做的是从 imageView 中删除图像并重新设置图像。对我有用。
cell.imageView.image = nil
cell.imageView.image = UIImage(named:"Calendar")