UITableView reloadRowsAtIndexPathsgraphics化的故障

如果我为段的第一个单元调用reloadRowsAtIndexPaths,前面的部分是空的,而上面的部分不是空的,我得到一个奇怪的animation毛刺(即使我指定“UITableViewRowAnimationNone”)重新加载的单元格从上面的部分滑落。 。

我试图尽可能简化这个例子:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) return 1; else if (section == 1) return 0; else if (section == 2) return 3; return 0; } - (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] autorelease]; } // Configure the cell... cell.textLabel.text = @"Text"; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *editedCell = [[NSArray alloc] initWithObjects:indexPath, nil]; //[self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:editedCell withRowAnimation:UITableViewRowAnimationNone]; //[self.tableView endUpdates]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return @"Section"; } 

其实你可以评论一下最后一个方法,但是它能更好的理解这个问题。

您可以直接设置想要的单元格的值,而不是让表重新加载(从而避免任何不需要的animation)。 为了使代码更清晰并避免代码重复,可以将单元格设置移到单独的方法(所以我们可以从不同的位置调用它):

 - (void) setupCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath { cell.textLabel.text = @"Text"; // Or any value depending on index path } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [self setupCell:cell forIndexPath:indexPath]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // create cell // Configure the cell... [self setupCell:cell forIndexPath:indexPath]; return cell; }