IOS development – UITableView optimized cache cell height

IOS development - UITableView optimized cache cell height
SNSD – Lim Yoona

Why cache height?

Because when tableView rolling will not stop the callback – (CGFloat) tableView: (UITableView * tableView) heightForRowAtIndexPath: (NSIndexPath * indexPath); the agent method, when the level adaptive content of cell, it means that every time the method to calculate the callback height, and calculation is to spend time, reflected in the user experience is Caton. In order to avoid repeated and meaningless calculation of cell height, the cache height is particularly important.

How to cache?

The need for a highly variable cache array, every time – (CGFloat) tableView: (UITableView * tableView) heightForRowAtIndexPath: (NSIndexPath * indexPath); this way, we’ll go to the array to take, if any, just take it out, if not, the calculation height, and put them into an array.

Code explanation

  • This is we put a highly variable array cache cache array cell height / * * * / @property (nonatomic, strong) NSMutableArray *heightArray;
  • Note that lazy loading (NSMutableArray * heightArray{) if (_heightArray = = Nil) {_heightArray} = [NSMutableArray array]; return _heightArray;}
  • Cache height (CGFloat) tableView: (UITableView * tableView) heightForRowAtIndexPath: (NSIndexPath *) indexPath{CGFloat if (self.heightArray.count height; > indexPath.row) {/ / if there is a cache cache out height, height height = [self.heightArray[indexPath.row] floatValue];}else{; / / no cache height, calculating the height, and adding an array of height according to the comments / / how many adaptive CQGoodsCommentModel *model = self.dataArray[indexPath.row] CGFloat contentWidth = screenWidth-20; / / column; / / what font to display UIFont *font [UIFont = systemFontOfSize:13]; CGSize = [model.comment_content size / / size sizeWithFont:font constrainedToSize:CGSizeMake (contentWid Th lineBreakMode:UILineBreakModeWordWrap], 1000); / / return here need height height = size.height+60; / / [self.heightArray addObject:[NSNumber numberWithDouble:height]]} adding array; return height;}
  • Remember to refresh the tableView empty cache array set self.contentTableView.header header height = [CQHeader headerWithRefreshingBlock:^{/ / _page = 1; [self.heightArray removeAllObjects]; [self getDataIsRefresh:YES];}];

summary

Generally speaking, the height of the cache cell is still relatively simple, just add a variable array on the basis of the original, the logic is not complicated. However, the optimization of its performance can not be underestimated, especially when the cell content is complex and highly adaptive content. It is strongly recommended that everyone get this technology, even if it is an entry-level developer.