2

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?

2 Answers2

7

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 4
    You already have some data structures used for each row in the table, correct? Add an additional BOOL value to this data to keep track of whether the row is expanded or not. – rmaddy Jan 26 '13 at 18:25
  • Model-view-controller is probably the thing to read up on. Don't put model inside your views. – Tommy Oct 30 '15 at 14:59
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];
}
EricS
  • 9,650
  • 2
  • 38
  • 34