How to reload UITableView without stopping cell selection animation

匿名 (未验证) 提交于 2019-12-03 01:04:01

问题:

When the users taps on a cell, I want to update my UITableView; including the contents of this tapped cell. Easiest way is to update internal parameters and then invoke [self.tableView reloadData];.

However, reloadData immediately stops the nice blue->none selection animation of my tapped cell.

Is there a (standard) way to update my table cells without stopping the tapped cell's animation?

Note in this case I don't add or delete cells; I just want to contents to change (e.g. start an activity indicator, or change the color of labels.)

回答1:

In your case you can just get pointers to all visible cells and update them. Something like this:

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {     NSArray* visibleCells = [tableView indexPathsForVisibleRows];      for (NSIndexPath* indexPath in visibleCells)     {         UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];          [self updateCell:cell atIndexPath:indexPath]; // Your method, which updates content...     } }

And if you want to update other cells content you can use something like this:

- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath {     [self updateCell:cell atIndexPath:indexPath]; // Your method, which updates content... }

So your cells will always display correct content.

About creating content:

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString* CellIdentifier = @"Cell";     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];      if (cell == nil)     {         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];          [self createContentForCell:cell atIndexPath:indexPath]; // so here to create content or customize cell     }      return cell; }


回答2:

Or maybe you could simply delay the reload of your table data as described for example here: Delay reloadData on UITableView



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