10

如果我为一个部分的第一个单元格调用 reloadRowsAtIndexPaths,前一个部分是空的,而上面的一个不是空的,我会得到一个奇怪的动画故障(即使我指定“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";
}

实际上,您可以注释掉最后一种方法,但它可以更好地理解问题。

4

1 回答 1

13

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

- (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;
}
于 2011-09-08T09:33:40.853 回答