[英]UITableViewCell cell reuse and UILabel auto-layout dynamic sizing
[英]UIImageView in a UITableViewCell and auto layout not working on cell reuse
細胞在啟動時看起來正確。
滾動到新單元格(顯然會調用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上的約束:
每次設置圖像后都需要調用setNeedsLayout和layoutIfNeeded,並且每次重復使用時必須將ImageView.image設置為nil。 :|
class UserGroupCell: UITableViewCell {
@IBOutlet weak var userGroupNameLabel: UILabel!
@IBOutlet weak var userGroupPhotoImageView: UIImageView! {
didSet {
userGroupPhotoImageView.clipsToBounds = true;
userGroupPhotoImageView.layer.masksToBounds = true;
userGroupPhotoImageView.layer.cornerRadius = 5.0;
}
}
override func prepareForReuse() {
super.prepareForReuse()
userGroupPhotoImageView.image = nil
}
func updateWithImage(image: UIImage) {
self.userGroupPhotoImageView.image = image
self.setNeedsLayout()
self.layoutIfNeeded()
}
}
如果你沒有找到任何答案。 你應該以編程方式進行。 並在中設置約束
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
方法。 這樣你就可以確定會發生什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.