【发布时间】:2015-04-22 22:11:08
【问题描述】:
细胞在启动时看起来是正确的。
一旦您滚动到一个新单元格(显然 dequeReusableCell 被调用),该单元格上先前存在的约束就会丢失。
这是单元格中 imageView 的约束设置:
这是应用启动时的样子(单元格按我的意愿布局)
当您开始滚动时,单元格的重复使用会破坏约束。
在单元格上设置图像后,我在 updateImage 方法中调用 layoutIfNeeded():
func updateWithImage(image: UIImage) {
userGroupPhotoImageView.layer.masksToBounds = true;
userGroupPhotoImageView.layer.cornerRadius = 5.0;
self.userGroupPhotoImageView.image = image
self.layoutIfNeeded()
}
indexPath 处的行的单元格:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as MyCell
cell.nameLabel?.text = "Cell \(indexPath.row)"
switch (indexPath.row) {
case 0:
cell.updateWithImage(UIImage(named: "group0")!)
case 1:
cell.updateWithImage(UIImage(named: "group1")!)
case 2:
cell.updateWithImage(UIImage(named: "group2")!)
case 3:
cell.updateWithImage(UIImage(named: "group3")!)
case 4:
cell.updateWithImage(UIImage(named: "group4")!)
case 5:
cell.updateWithImage(UIImage(named: "IMG_0184")!)
default:
cell.updateWithImage(UIImage(named: "IMG_0185")!)
}
return cell
}
开始滚动后的样子:
向下滚动:
向上滚动到顶部;
ImageView 的约束:
【问题讨论】:
-
我不清楚你为什么使用 updateWithImage: 方法。前两行只需要执行一次,最好在单元格的初始化代码中完成(并且您根本不需要 layoutIfNeeded 行)。
-
所以需要在 prepareForReuse() 上将 imageView.image 设置为 nil 我想知道为什么会这样。
标签: ios objective-c iphone uitableview swift