以编程方式向TableView数据源添加/删除数据

时间:2010-08-24 06:14:54

标签: iphone objective-c uitableview three20

HI @ll!

我想以编程方式为我的tableview添加和删除一些项目到我的数据源。

首先:我想达到什么目的? 我有一个带有TTTableView控件的UIView(来自Three20项目的Tableview,但派生自常见的UITableView控件)。这个TableView运行良好,并且附加了一个TTSegmentedDataSource对象作为数据源。目前,TableView正在显示UISwitch组件。我想要做的是,根据交换机的状态显示和隐藏UISwitch控件上方的一些项目。元素应隐藏和显示使用常见的TableView动画,就像用户添加或删除项目一样。

我已经尝试过严厉的方法,但我没有得到它。

例如:

[self.treeView beginUpdates];
[[((TTSegmentedDataSource*)self.treeView.datasource).items objectAtIndex:0] removeObject: objectToBeRemoved];
[self.treeView endUpdates];
[self.treeView reloadData];

这不起作用,也引发了例外。

解决方案是什么?如何以编程方式在TableView中添加和删除项目?

希望你能帮助我! THX!

C YA

3 个答案:

答案 0 :(得分:1)

尝试查看:

-(void)insertSections:(NSIndexSet *)sections withRowAnimation:UITableViewRowAnimation)animation
-(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

答案 1 :(得分:1)

感谢您提示cooltechnomax。

我找到了解决方案。这是一个例子:

[states removeObjectAtIndex:4]; // Washington
[states removeObjectAtIndex:2]; // Delaware

[states insertObject:@"Alaska" atIndex:0];
[states insertObject:@"Georgia" atIndex:3];
[states insertObject:@"Virginia" atIndex:5];

NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
                            [NSIndexPath indexPathForRow:2 inSection:0],
                            [NSIndexPath indexPathForRow:4 inSection:0],
                            nil];

NSArray *insertIndexPaths = [NSArray arrayWithObjects:
                            [NSIndexPath indexPathForRow:0 inSection:0],
                            [NSIndexPath indexPathForRow:3 inSection:0],
                            [NSIndexPath indexPathForRow:5 inSection:0],
                            nil];

UITableView *tv = (UITableView *)self.view;

[tv beginUpdates];

[tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
[tv deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];

[tv endUpdates];

答案 2 :(得分:0)

如何修改模型并致电"reloadData" [tv reloadData]

相关问题