Dir*_*sch 5 iphone uitableview
我有一个UITableView与交替着色的UITableViewCells.并且可以编辑表:可以重新排序和删除行.当行被重新排序或删除时,如何更新交替背景颜色的单元格?
我正在用它绘制交替的彩色单元格:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([indexPath row] % 2) {
// even row
cell.backgroundColor = evenColor;
} else {
// odd row
cell.backgroundColor = oddColor;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当重新排序或删除行时,不会调用此方法.我不能[tableView reloadData]从以下方法调用,因为它在无限循环中崩溃应用程序:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
// Move the object in the array
id object = [[self.list objectAtIndex:[fromIndexPath row]] retain];
[self.list removeObjectAtIndex:[fromIndexPath row]];
[self.list insertObject:object atIndex:[toIndexPath row]];
[object release];
// Update the table ???
[tableView reloadData]; // Crashes the app in an infinite loop!!
}
Run Code Online (Sandbox Code Playgroud)
是否有人有指针或最佳实践解决方案来处理重新排序交替的彩色细胞的问题?
谢谢
如果您无法从该方法调用,则使用延迟调用来执行重新加载:
[tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.0f];
Run Code Online (Sandbox Code Playgroud)
它等待你的当前方法完成之后再调用reload.
无需使用第三方对象或重新加载/刷新整个数据源。只需在您的瑞士刀中使用正确的工具:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//1. remove your object
[dataSource removeObjectAtIndex:indexPath.row];
//2. update your UI accordingly
[self.myTableView beginUpdates];
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self.myTableView endUpdates];
//3. obtain the whole cells (ie. the visible ones) influenced by changes
NSArray *cellsNeedsUpdate = [myTableView visibleCells];
NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
for(UITableViewCell *aCell in cellsNeedsUpdate) {
[indexPaths addObject:[myTableView indexPathForCell:aCell]];
}
//4. ask your tableview to reload them (and only them)
[self.myTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3350 次 |
| 最近记录: |