Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

ios - How do I tell my UITableViewCell to "auto-resize" when its content changes?

In my cell.xib, I have a label, with constraints to all its sides. I've set that label to lines = 0 and line-break = word wrap. Then, I do this to my TableView:

self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100.0

Everything works great, and my UITableViewCell is auto-height. If the text is long, then my tableView intelligently calculates the size.

The problem is -- how do I tell my UITableView to "re-calculate" the size once the content changes in the cell?

My cell could call its delegate, and in this delegate, I'd like the TableView to re-draw the height.

Right now, the content in my cells change constantly, but the cell height never changes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a documented way to do this. See UITableView.beginUpdates() documentation:

You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

So, the correct solution is:

tableView.beginUpdates()
tableView.endUpdates()

Also note that there is a feature that is not documented - you can add a completion handler for the update animation here, too:

tableView.beginUpdates()
CATransaction.setCompletionBlock {
   // this will be called when the update animation ends
}

tableView.endUpdates()

However, tread lightly, it's not documented (but it works because UITableView uses a CATransaction for the animation).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...