在 tableview 单元格中加载和缓存多个 UIImageView 不起作用
Loading and caching multiple UIImageView in a tableview cell not working
我有一个带有 3 个 UIImageView 的自定义 table 视图单元格。我使用 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
}
自定义 table 视图单元格:
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
}
}
...
}
我的 table 视图控制器:
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
}
您从未将隐藏的 UIImageView 设置为重新出现,因此它们似乎只有在重新使用时才会隐藏。
将您的代码更改为
func configureCell() {
profilePic3.hidden = false
profilePic2.hidden = false
setUsersImage()
}
我有一个带有 3 个 UIImageView 的自定义 table 视图单元格。我使用 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
}
自定义 table 视图单元格:
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
}
}
...
}
我的 table 视图控制器:
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
}
您从未将隐藏的 UIImageView 设置为重新出现,因此它们似乎只有在重新使用时才会隐藏。
将您的代码更改为
func configureCell() {
profilePic3.hidden = false
profilePic2.hidden = false
setUsersImage()
}