2009-08-23 43 views
5

我有一个交替着色的UITableViewCells的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; 
    } 
} 

但是,当一排被重新排序或删除不会被调用此方法。我不能调用从以下方法[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!! 
} 

是否有人有一个指针或最佳实践的解决方案,以应对重排交替有色细胞的问题?

谢谢

回答

5

用于延迟呼叫进行重装,如果你不能从方法调用:

[tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.0f]; 

它等待后调用重装前当前的方法完成,直到。

+0

谢谢!这就是诀窍。 – 2009-08-24 10:54:06

0

[tableView reloadData]会让你的表格背景重新回到事物的摆动中。另一种选择是将所有可见单元格的背景颜色从移动中较低索引的indexPath交换到visibleCells中最高的索引。

3

重载太重了;我写了AltTableViewController,它只是改变单元格的背景颜色,它应该工作得更快。

+0

谢谢,这很好! – 2011-02-09 21:40:07

1

我从tableview中取出所有UITableViewCell子视图,并根据单元格frame.origin.y对该数组进行排序,以便它们按正确的顺序返回。然后我通过改变基于索引== 0 ||的背景颜色来循环播放索引%2 == 0重新着色它们。似乎比重新加载tableView更好地工作,因为这是导致动画混乱。在凌晨1:25为我工作

4

无需使用第三方对象或重新加载/刷新整个数据源。只需在你的瑞士刀中使用正确的工具:

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

    } 
} 
+1

'NSArray * indexPaths = [self.tableView indexPathsForVisibleRows];'将您从一个不必要的循环中拯救出来。 – user3099609 2014-02-26 17:25:42

0

这很好。从最后一个索引开始,而不是第一个。这样每个单元总是保留它的背景:

if (dataSource.count - indexPath.row) % 2 == 0 { 
     cell.backgroundColor = UIColor.grayColor() 
    } else { 
     cell.backgroundColor = UIColor.whiteColor() 
    } 

我试过其他的解决方案,但并没有完全满意。该解决方案不是黑客行为,甚至不添加另一行代码。

相关问题