如何正确使用insertRowsAtIndexPaths? [英] how to properly use insertRowsAtIndexPaths?

查看:832
本文介绍了如何正确使用insertRowsAtIndexPaths?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线查看了所有示例,无法弄清楚如何使用动画正确添加单元格到tableview。假设我有一个包含一个单元格的部分,并且我想在用户点击第一个单元格的附件时添加另一个单元格。

I went through all the examples online and could not figure out how to properly add a cell to a tableview with animation. Let's say I have one section with one cell and I want to add another cell once the user clicks on the first cell's accessory.

我的添加方法执行此操作:

My "add" method does this:

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

}

哪个有效,但没有动画。我明白为了添加动画,我需要使用insertRowsAtIndexPaths:withRowAnimation,所以我尝试了很多选项但是在执行insertRowsAtIndexPaths:withRowAnimation方法时它总是崩溃。

Which is working but there is no animation. I understand that in order to add animation, I need to use insertRowsAtIndexPaths:withRowAnimation, so I tried tons of options but it always crashes when executing the insertRowsAtIndexPaths:withRowAnimation method.

我最近的尝试是这样做的:

My recent try was by doing this:

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

}
}  

我做错了什么?我怎样才能轻松实现这一目标?我不明白这整个indexPathForRow的事情......我也不明白如何使用这种方法我可以为新单元格添加标签名称。请帮助...谢谢!!

What am I doing wrong? How can I make this happen easily? I dont understand this whole indexPathForRow thing...I also dont understand how with this method I can add a label name to the new cell. Please help...thanks!!

推荐答案

使用 insertRowsAtIndexPaths时要记住的重要事项/ code>是你的 UITableViewDataSource 需要匹配插件告诉它做的事情。如果向表视图添加行,请确保已更新后备数据以匹配。

The important thing to keep in mind when using insertRowsAtIndexPaths is that your UITableViewDataSource needs to match what the insert is telling it to do. If you add a row to the tableview, make sure the backing data is already updated to match.

这篇关于如何正确使用insertRowsAtIndexPaths?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆