使用动画在UITableView中添加单元格

Mar*_*cal 0 iphone animation uitableview ios

我有一个UITableView.我从NSDictionary中获取它,其中包含表格中每组项目的数组:标签,页脚,标题,UIViews等.

在第0部分中,我希望在第1行中的开关切换为开启时出现第2行.

我所做的和它的工作原理是,在numberOfRowsInSection中我添加了这段代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (interruptor.isOn==NO && section==0) {
    return [[[infoTableContentArray objectAtIndex: section] objectForKey:kLabelKey] count]-1;
}else{

    return [[[infoTableContentArray objectAtIndex: section] objectForKey:kLabelKey] count]; 

}
Run Code Online (Sandbox Code Playgroud)

}

并且链接到开关(中断器)的动作是:

-(IBAction)accioInterruptor:(id)sender{

[infoAndSettingsTable reloadData];
Run Code Online (Sandbox Code Playgroud)

}

因此,当切换开关时,表重新加载并且单元格出现或消失.它确实有效,但没有动画,这就是它,嗯......嗯,你知道.

我试图实现reloadRowsAtIndexPaths:withRowAnimation,将其添加到交换机调用的代码中:

-(IBAction)accioInterruptor:(id)sender{



[infoAndSettingsTable beginUpdates];
[infoAndSettingsTable reloadRowsAtIndexPaths:[[infoTableContentArray objectAtIndex: 0] objectForKey:kLabelKey] withRowAnimation:UITableViewRowAnimationBottom];
[infoAndSettingsTable endUpdates];
Run Code Online (Sandbox Code Playgroud)

}

但是,它不起作用.它崩溃了[infoAndSettingsTable endsUpdates];

顺便说一句,在所有情况下如下:

[[infoTableContentArray objectAtIndex: 0]
Run Code Online (Sandbox Code Playgroud)

是包含该部分标签的数组.

我做得对吗还是我史诗般的失败?

提前致谢!

Nir*_*iya 6

简单的方法来做到这一点......

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

insertIndexPaths是要插入到表中的NSIndexPaths数组.

deleteIndexPaths是要从表中删除的NSIndexPaths数组.

索引路径的示例数组格式:

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
        [NSIndexPath indexPathForRow:0 inSection:0],
        [NSIndexPath indexPathForRow:1 inSection:0],
        [NSIndexPath indexPathForRow:2 inSection:0],
        nil];
Run Code Online (Sandbox Code Playgroud)

这个问题得到它