Tom*_*myG 33 iphone xcode objective-c uitableview
我在网上浏览了所有的例子,无法弄清楚如何正确地将一个单元格添加到带动画的tableview中.假设我有一个部分有一个单元格,我想在用户点击第一个单元格的附件后添加另一个单元格.
我的"添加"方法执行此操作:
- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender {
if (switch1.on) {
NSArray *appleComputers = [NSArray arrayWithObjects:@"WWWWW" ,@"XXXX", @"YYYY", @"ZZZZ", nil];
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:@"Computers"];
[listOfItems replaceObjectAtIndex:0 withObject:appleComputersDict];
[tblSimpleTable reloadData];
}
Run Code Online (Sandbox Code Playgroud)
哪个有效,但没有动画.我知道为了添加动画,我需要使用insertRowsAtIndexPaths:withRowAnimation,所以我尝试了很多选项但是在执行insertRowsAtIndexPaths:withRowAnimation方法时它总是崩溃.
我最近的尝试是通过这样做:
- (IBAction) toggleEnabledTextForSwitch1onSomeLabel: (id) sender {
if (switch1.on) {
NSIndexPath *path1 = [NSIndexPath indexPathForRow:1 inSection:0]; //ALSO TRIED WITH indexPathRow:0
NSArray *indexArray = [NSArray arrayWithObjects:path1,nil];
[tblSimpleTable insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight];
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我怎样才能轻松实现这一目标?我不明白这整个indexPathForRow的事情......我也不明白如何使用这种方法我可以为新单元格添加标签名称.请帮忙......谢谢!!
Ter*_*cox 23
这是一个两步过程:
首先更新您的数据源,numberOfRowsInSection
然后cellForRowAtIndexPath
返回插入后数据的正确值.您必须在插入或删除行之前执行此操作,否则您将看到"无效行数"错误.
然后插入你的行:
[tblSimpleTable beginUpdates];
[tblSimpleTable insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight];
[tblSimpleTable endUpdates];
Run Code Online (Sandbox Code Playgroud)
简单地插入或删除行不会改变您的数据源; 你必须自己做.
Jos*_*erg 22
使用时要记住的重要一点insertRowsAtIndexPaths
是,您UITableViewDataSource
需要匹配插件告诉它要做的事情.如果向表视图添加行,请确保已更新后备数据以匹配.
首先,您应该在更新表本身之前更新数据模型.你也可以使用:
[tableView beginUpdates];
// do all row insertion/delete here
[tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
并且表将使用动画一次性生成所有更改(如果您指定)
归档时间: |
|
查看次数: |
46829 次 |
最近记录: |