views:

140

answers:

2

HI @ll!

I want to add and remove some items to my datasource for my tableview programmatically.

First of all: What do I want to achieve? I habe a UIView with a TTTableView control (the Tableview from the Three20 project, but derived from the common UITableView control). This TableView is working well and has a TTSegmentedDataSource object attached as datasource. Currently the TableView is showing a UISwitch component. What I want to do is, to show and hide some items above the UISwitch control according to the state of the switch. And the elements should hide and show using the common TableView animations, just like the user has added or removed an item.

I already tried severeal aproaches to do so, but I don't get it.

For example:

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

This didn't work, and also throwed an exception.

What is the solution? How can I programmatically add and remove items from a TableView?

Hoping you can help me! THX!

C YA

+1  A: 

Try looking into:

-(void)insertSections:(NSIndexSet *)sections withRowAnimation:UITableViewRowAnimation)animation
-(void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
cooltechnomax
+1  A: 

Thanks for your hint cooltechnomax.

I found the solution. Here is an example:

[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];
Nuker
The idea of `beginUpdates` and `endUpdates` is that during an 'update block', you change both your data source, and your tableview, and at the end of the block, they should be in sync. You should move the statements modifying your `states` array inside the `beginUpdates`/`endUpdates` block, so the only time that your dataSource and tableview are out of sync is during the 'updates block'.
Nick Forge
Really? But this is from the official Apple documentation...http://developer.apple.com/iphone/library/documentation/userexperience/conceptual/TableView_iPhone/ManageInsertDeleteRow/ManageInsertDeleteRow.html(Listing 7-8)
Nuker