Ios UITableView重新加载行显示图形故障
如果我为节的第一个单元格调用reloadRowsAtIndexPaths,前一个节为空,上面的一个不为空,我会遇到一个奇怪的动画故障(即使我指定“UITableViewRowAnimationNone”),其中重新加载的单元格会从上一节向下滑动 我试图尽可能地简化示例:Ios UITableView重新加载行显示图形故障,ios,uitableview,animation,visual-glitch,Ios,Uitableview,Animation,Visual Glitch,如果我为节的第一个单元格调用reloadRowsAtIndexPaths,前一个节为空,上面的一个不为空,我会遇到一个奇怪的动画故障(即使我指定“UITableViewRowAnimationNone”),其中重新加载的单元格会从上一节向下滑动 我试图尽可能地简化示例: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITab
- (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";
}
实际上,您可以注释掉最后一个方法,但它可以更好地理解问题。您可以直接为单元格设置值,而不让表格重新加载自身(从而避免任何不需要的动画)。另外,为了使代码更清晰并避免代码重复,让我们将单元格设置移动到一个单独的方法(这样我们就可以从不同的位置调用它):
这是一个很好的解决方案,不管怎么说,是重新加载的奇怪行为暴露了一个bug,还是我只是用错了方法?@Fr4ncis,我不确定。要重新加载单元格,表视图可能需要从视图层次结构中添加/删除单元格,重建其部分子视图或其他内容-这取决于所有这些转换是如何在内部实现的谢谢,您的解决方案是干净的、结构良好的。多好的解决方案啊!天才。非常感谢你。
- (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;
}