[英]How to find and set the text to UILabel of UITableViewCell in Swift?
[英]Swift - UITableViewCell jumps when updating UILabel text
当我像这样更新 UITableViewCell 中的 UILabel 文本时:
self.label.text = "label text"
一切都很好......
但是当我从Struct Array更新我的标签文本时,它会导致 UITableView 在向上滚动到顶部时有时会跳转,而不是在向下滚动时,而是当我向顶部滚动时......
无法理解为什么会发生这种情况......
结构,数据是从我从服务器得到的结果中添加的
struct ContentStruct {
var textLink : String? = ""
var title : String? = ""
var text : String? = ""
}
单元格换行
let postData = cellContentArray[(indexPath as NSIndexPath).section]
cell.binData(content: postData)
表视图单元格
func binData(content: ContentStruct) {
self.titleLabel.text = content.title
self.text.text = "yytytytyt"//text
self.uuidLabel.text = "yhhyhyhyyh"
}
我正在像这样调整单元格的大小:在 ViewDidLoad 中:
tableView.rowHeight = UITableViewAutomaticDimension
然后这个:
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)
}
有任何想法吗?
提前致谢!
我发现当您的动态单元格高度与您估计的行高不匹配时,会发生这种跳跃。 最简单的解决方法是有一个固定的单元格高度,或者单独计算每个单元格的高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.