ios - UITableView reloadRowsAtIndexPaths 图形故障

标签 ios uitableview animation visual-glitch

如果我为一个部分的第一个单元格调用 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";
}

其实你可以把最后一个方法注释掉,但这样可以更好地理解问题。

最佳答案

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

- (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;
}

关于ios - UITableView reloadRowsAtIndexPaths 图形故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7345802/

相关文章:

ios - 如何从通知区域中删除特定的远程通知

ios - Firebase 在 Swift iOS 中将我的 Double 检索为字符串?

android - 在android中从右上角、左下角、右下角旋转动画android?

animation - Flutter 上的 Lottie 闪屏

javascript - 点击时 react 模态动画

ios - 如何在 ios 强制门户弹出浏览器上播放视频

objective-c - 在 iOS 中播放整个程序中重复的声音

swift - 使用 NSFetchRequest 从 CoreData 填充 TableView

ios - 如何通过滑动手势获取 IndexPath 和 UItableView

swift - Parse.com Swift 的搜索功能