2009-07-08 85 views
2
NSIndexPath* updatedPath = [NSIndexPath indexPathForRow: 0 inSection: 0]; 
NSIndexPath* updatedPath2 = [NSIndexPath indexPathForRow: 1 inSection: 0]; 
NSArray* updatedPaths = [NSArray arrayWithObjects:updatedPath, updatedPath2, nil]; 
[self.mySexyTableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationTop]; 

上面的代码工作,它动画。问题是我有很多数据,而且我不想对行索引进行硬编码。由于我的表格部分只有一个部分可以是0.我该如何动态生成NSIndexPath对象的NSArray?UITableView动画头痛

或者是否有更简单的方法来动画表视图。当用户点击表格视图顶部的标签时,我的表格视图中的所有行都会更改。

回答

7

要生成的索引路径数组,你可以只循环:

NSMutableArray *updatedPaths = [NSMutableArray array]; 
    for (NSNumber *row in someArray) { 
     NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:[row intValue] inSection:0]; 
     [updatedPaths addObject:updatedPath]; 
    } 
    [self.mySexyTableView reloadRowsAtIndexPaths:updatedPaths withRowAnimation:UITableViewRowAnimationTop]; 

如果你运行你的TableView中的所有数据,为什么不直接调用reloadData方法?

+3

reloadData不动画。 – bparanj 2009-07-08 16:08:50

0

我终于搞定了。下面是代码:

NSMutableArray *indexPaths = [[[NSMutableArray alloc] init] autorelease]; 
for (int i = 0; i < [mySexyList count]; i++) { 
    NSIndexPath *updatedPath = [NSIndexPath indexPathForRow:i inSection:0]; 
    [indexPaths addObject:updatedPath]; 
} 

[self.myFunkyTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop]; 

的问题是,我是从返回硬numberOfRowsInSection编码值:方法,其是不一样mySexyList大小。这是崩溃的应用程序。

3

如果您有多个具有相同行索引的indexPathForRow,则会出现此异常。

对于erample

[segmentTable reloadRowsAtIndexPaths: [[NSArray alloc] initWithObjects: 
           [NSIndexPath indexPathForRow: 1 inSection: 1], 
           [NSIndexPath indexPathForRow: 1 inSection: 1], nil]         withRowAnimation:UITableViewRowAnimationNone]; 
6

如果要刷新一整节:

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop]; 
0

假设self.arTableData是表的可变数组& tableViewUITableView一个实例。假设tableView中有10行(表示10个阵列中的对象)。 我会执行以下代码以删除动画行。

int i; // to remove from 3 to 6 
for(i=2;i<6;i++) 
{ 
    [self.arTableData removeObjectAtIndex:i]; 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:i inSection:0]] withRowAnimation:UITableViewRowAnimationRight]; 
} 

那个技巧对我来说没有任何麻烦。希望它对你有同样的作用。祝你好运。

0

只需将我的解决方案添加到混音中,因为一旦找到它就很简单。发生了什么事情是,我有一张桌子,按照我自己的代码使用以下相同的技术进行“分组”:cocoanetics.com

当用户执行某项操作时需要更新单元格,并且表格未分组那时候,我的代码试图使用

[[self tableView] reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationAutomatic]; 

指定到单元格和不存在的标题的路径。这导致了其他人上面报告的相同错误。我所要做的就是确保提供给reloadRowsAtIndexPaths:withRowAnimation:的“路径”实际存在。这样做,一切都很好。