Reuse an UITableViewCell (customized height for each one) in a twitter-like client
-
12-11-2019 - |
题
I am working on a project like twitter client iOS version. When designing the tableview, every cell changes the height to accommodate its content (tweet).
But I met a problem: the current screen's cell height works well, but when scroll down, the new cell reuses the height of old cells.
解决方案
Yes, table programming is where model-view-controller approach really comes usefull. If you change your cell height somewhere like tableView:didDeselectRowAtIndexPath:
you could get a feeling that it works - but it doesn't. What you can do in this method is modify your data and reload the table (or part of it).
As mentioned in comments above you'll have to:
Provide correct height for each cell in
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
Provide correct view (cell) in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Whenewer you want to make some change you should modify your data in model
(or appropriate object holding your content-related data) and then call one of the UITableView's reload
methods, possibly:
- (void)reloadData
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
Apple's documentation can be scarce (though i think it's quite proffesional): These would be the basic links for you:
UITableViewDataSource Protocol Reference