【问题标题】:UITableView reloadRowsAtIndexPaths graphical glitchUITableView reloadRowsAtIndexPaths 图形故障
【发布时间】:2011-09-08 09:20:11
【问题描述】:

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

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

【问题讨论】:

    标签: ios uitableview animation visual-glitch


    【解决方案1】:

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

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

    【讨论】:

    • 无论如何,这是一个很好的解决方案,reloadRowsAtIndexPaths 的奇怪行为是一个错误,还是我只是使用错误的方式?
    • @Fr4ncis,我不确定。要重新加载单元格,表视图可能需要从视图层次结构中添加/删除它们,重建它的一些子视图或其他东西 - 这取决于所有这些转换是如何在内部实现的
    • 谢谢,您的解决方案结构清晰。
    • 多么好的解决方案!天才。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 2014-06-24
    • 1970-01-01
    • 2013-11-04
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多