uitableview: create a collapsible tableview (can be used for dropdown menus)
I am a newbie to iphone development and is not able to understand various methods and all. I want to create a collapsible table view which expands and collapse on click of a button.
I have following pieces in my puzzle
1) I guess I will have to use these two methods for animation
[collapsibleTableView deleteRowsAtIndexPaths:editableIndexArray withRowAnimation:UITableViewRowAnimationTop ];
[collapsibleTableView insertRowsAtIndexPaths:editableIndexArray withRowAnimation:UITableViewRowAnimationTop ];
but I am not sure about argument which I need to pass to de开发者_JAVA百科leteRowsAtIndexPath:
method and insertRowsAtIndexPath:
method.
2) I have an array which is the datasource for this table.
It would be great if you can make me understand how it exactly needs to be done.
got the answer and the implementation is
- (NSArray*)indexPathsInSection:(NSInteger)section {
NSMutableArray *paths = [NSMutableArray array];
NSInteger row;
for ( row = 0; row < [self numberOfRowsInSection:section]; row++ ) {
[paths addObject:[NSIndexPath indexPathForRow:row inSection:section]];
}
return [NSArray arrayWithArray:paths];
}
-(NSInteger)numberOfRowsInSection:(NSInteger)section {
return 4;
}
-(IBAction)expandCollapseMenu {
NSArray *paths = [self indexPathsInSection:0];
if (isExpanded) {
isExpanded=NO;
[actionBtn setTitle:@"Expand" forState:UIControlStateNormal];
[menuTblView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
}
else {
isExpanded=YES;
[actionBtn setTitle:@"Collapse" forState:UIControlStateNormal];
[menuTblView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade ];
}
}
By expands and contracts do you simply mean removing all entries (whole table or by section?), or do you mean animating the removal/addition of the tableview from the screen.
If you mean removing/adding rows, then your best bet is to likely have an array that holds all your real valid data and an array that holds the "This is what the user can see" data - so you'll clear it or parts of it when you remove/add rows.
To get the indexPaths, you can use [NSIndexPath indexPathForRow:<#(NSUInteger)#> inSection:<#(NSUInteger)#>]
to create the individual paths and shove them into an array which you pass to that method.
Just make sure that you maintain the "This is what the user can see" array when you add/remove rows as when the tableview shows cells it will request your underlying data.
精彩评论