当我像这样更新 UITableViewCell 中的 UILabel 文本时:
self.label.text = "label text"
一切都很好......
但是当我从结构数组更新标签文本时,它会导致 UITableView 有时在向上滚动到顶部时跳转,而不是向下滚动时跳转,而是当我向顶部滚动...
无法理解为什么会发生这种情况......
结构,正在从服务器获取的结果中添加数据
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"
}
我正在调整单元格的大小,如下所示: 在 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)
}
有什么想法吗?
提前致谢!
最佳答案
我发现,当动态单元格高度与估计行高不匹配时,就会发生类似的跳跃。最简单的解决方法是固定单元格高度,或者单独计算每个单元格的高度。
关于ios - Swift - UITableViewCell 更新 UILabel 文本时跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41961314/