How can I disable UITableView's cells reloading when scrolling it?

时光总嘲笑我的痴心妄想 提交于 2020-01-02 09:09:50

问题


I've encountered with the problem when I don't need UITableView to reload its cells when I scroll it upwards and downwards. In simple words i created custom cell's which have content that user can expand by clicking on it. For example I have a UILabel with a lot of text in it which doesn't display fully but user can click on button and expand text and consequently a label. All this I implement via [tableview beginUpdates] and [tableview endUpdates]. But when I click button and expand text or image and scroll down to other cells and then up UITableView of course reload my cell's content and text is hiding again but cell remains the same size and this looks ugly. Can I in some way customise UITableView recycling mechanism or update internal cell cache?


回答1:


You cant disable the reloading. The proper solution is to ensure you are saving the state of each cell (expanded or not) so when the cell needs to be reloaded due to scrolling, you provide the proper height and content for the current state.




回答2:


You have a few options.

One that many people overlook is to not use a UITableView, but a UIScrollView instead. Just create each of your "cells" as a UIView subclass and place them where you want in the UIScrollView. The only issue is that you have to make it look like a tableview yourself, with separators between subviews and/or backgrounds on each cell.

Another option might be to keep your own reference to each Cell and tell UITableView not to reuse them. Something like:

- (void) initializeMyCells {
  self.cellArray = [NSMutableArray arrayWithCapacity:5];
  for ( int i = 0; i < 5; ++i ) {
     MyCellType *cell = [[MyCellType alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:nil]];
     [self.cellArray addObject:cell];
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  return self.cellArray[indexPath.row];
}


来源:https://stackoverflow.com/questions/14539977/how-can-i-disable-uitableviews-cells-reloading-when-scrolling-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!