在tableview单元格中加载和缓存多个UIImageView无法正常工作

时间:2016-08-31 07:00:12

标签: ios uitableview uiimageview haneke

我有一个带有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
}

1 个答案:

答案 0 :(得分:1)

您永远不会将隐藏的UIImageView设置为重新出现,因此它们似乎只有在重新发布时才会被隐藏。

将您的代码更改为

func configureCell() {
    profilePic3.hidden = false
    profilePic2.hidden = false
    setUsersImage()
}