简体   繁体   English

Swift - UITableViewCell 在更新 UILabel 文本时跳转

[英]Swift - UITableViewCell jumps when updating UILabel text

When I update my UILabel text in my UITableViewCell like this:当我像这样更新 UITableViewCell 中的 UILabel 文本时:

self.label.text = "label text"

Everything works great....一切都很好......

But when I update my labels text from the Struct Array it causes the UITableView to sometimes jump when scrolling up to the top, not when scrolling down, but when I scroll towards the top...但是当我从Struct Array更新我的标签文本时,它会导致 UITableView 在向上滚动到顶部时有时会跳转,而不是在向下滚动时,而是当我向顶部滚动时......

Cannot understand why this happens....无法理解为什么会发生这种情况......

Struct, data is being added from the results I get from the server结构,数据是从我从服务器得到的结果中添加的

struct ContentStruct {
  var textLink : String? = ""
  var title : String? = ""
  var text : String? = ""
}

CellForRow单元格换行

let postData = cellContentArray[(indexPath as NSIndexPath).section]

cell.binData(content: postData)

TableViewCell表视图单元格

 func binData(content: ContentStruct) {
    self.titleLabel.text = content.title
    self.text.text = "yytytytyt"//text
    self.uuidLabel.text = "yhhyhyhyyh"
   }

I am resizing the cell's like so: In the ViewDidLoad:我正在像这样调整单元格的大小:在 ViewDidLoad 中:

tableView.rowHeight = UITableViewAutomaticDimension

And then this:然后这个:

 func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    let height = self.heightAtIndexPath.object(forKey: indexPath)
    if ((height) != nil) {
        return CGFloat(height as! CGFloat)
    } else {
        return UITableViewAutomaticDimension
    }
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let height = cell.frame.size.height
    self.heightAtIndexPath.setObject(height, forKey: indexPath as NSCopying)
}

Any ideas?有任何想法吗?

Thanks in advance!提前致谢!

I've found that jumps like that occur when your dynamic cell heights don't match with your estimated row height.我发现当您的动态单元格高度与您估计的行高不匹配时,会发生这种跳跃。 Easiest fix is to have a fixed cell height, or to calculate the height individually per cell.最简单的解决方法是有一个固定的单元格高度,或者单独计算每个单元格的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM