【发布时间】:2017-06-29 20:34:30
【问题描述】:
在为cellForRowAtIndex 中的UITableViewCell 创建功能性图像缓存方面,我得到了很大帮助。不幸的是,使用下面的代码,只能一遍又一遍地显示一张图像。我的印象是 cellForRowAtIndexPath 就像一个 for 循环,为每个 row 再次运行。因此,我想知道为什么只显示一张图片。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "restaurantcell") as? RestaurantTableCell
var oneRestaurant: Restaurant = tablerestaurantarray[indexPath.row]
if let cachedVersion = cache.object(forKey: "image") {
oneRestaurant = cachedVersion
} else {
cache.setObject(oneRestaurant, forKey: "image")
}
cell?.picture?.image = oneRestaurant.value(forKey: "image") as! UIImage?
let restaurant = restaurantArray[indexPath.row]
cell?.name?.text = restaurant.value(forKey: "Name") as? String
return cell!
}
更新 2:
【问题讨论】:
-
单元格有多高?故事板中的默认图像是什么?据我了解,您可以从
Restaurant类中获得图像。你能把那门课也发一下吗? -
只是一个简单的“var image = UIimage?”为班级。
标签: ios swift uitableview caching tableviewcell