Reputation: 15
When I update my UILabel text in my UITableViewCell like this:
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...
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:
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!
Upvotes: 2
Views: 675
Reputation: 2725
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.
Upvotes: 2