我有一个带有 3 个 UIImageView 的自定义表格视图单元格。我使用Haneke异步加载和缓存它们。当我向上滚动时,一些以前看到的图像随机消失。这真的很奇怪。为什么会这样?
我也尝试过 SDWebImage,但问题仍然存在。
在 setUserProfileImage 中,我使用Haneke加载和缓存我的图像:
import Haneke
func setUserProfileImage(image: UIImageView, userImageId:String) {
let url = NSURL(string:"http://localhost:3000/api/v1/users/" + userImageId + "/pictures?access_token="+Const.headers["x-access-token"]!)
image.hnk_setImageFromURL(url!)
rectangleImage(image)
}
func rectangleImage(image: UIImageView) {
image.roundCorners()
image.clipsToBounds = true
}
自定义表格视图单元格:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var profilePic: UIImageView!
@IBOutlet weak var profilePic2: UIImageView!
@IBOutlet weak var profilePic3: UIImageView!
var cellGroup : Group = Group()
func configureGroup (m: Group) {
cellGroup = m
}
func configureCell() {
setUsersImage()
}
func setUsersImage() {
setUserProfileImage(profilePic, userImageId: cellGroup.users[0].substringFromIndex(cellGroup.users[0].startIndex.advancedBy(1)))
if cellGroup.users.count > 1 {
setUserProfileImage(profilePic2, userImageId: cellGroup.users[1].substringFromIndex(cellGroup.users[1].startIndex.advancedBy(1)))
if (cellGroup.users.count > 2) {
setUserProfileImage(profilePic3, userImageId: cellGroup.users[2].substringFromIndex(cellGroup.users[2].startIndex.advancedBy(1)))
}
else {
profilePic3.hidden = true
}
}
else {
profilePic2.hidden = true
profilePic3.hidden = true
}
}
...
}
我的表视图控制器:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell", forIndexPath: indexPath) as! CustomTableViewCell
reloadCellAtIndexPathRow(indexPath.row, cell: cell)
return cell
}
func reloadCellAtIndexPathRow(groupIndex : Int, cell: MomentTableViewCell) {
cell.configureGroup(groups[groupIndex])
cell.configureCell()
cell.selectionStyle = .None
cell.backgroundColor = nil
}