Dynamically adding row to iphone tableview

I need to add a new row in a tableview when I select a row in a table.

Suppose I use the following method:

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

What should I write inside the method to add a new line?

Please help me,

Thanks in advance Shibin.

+5
iphone tableview row
source share
4 answers

When I add a line dynamically, I use insertRowsAtIndexPaths: here is an example function

 - (void) allServersFound { // called from delegate when bonjourservices has listings of machines: bonjourMachines = [bonjourService foundServers]; // NSArray of machine names; NSMutableArray *tempArray = [[NSMutableArray alloc] init]; int i = 0; for (NSArray *count in bonjourMachines) { [tempArray addObject:[NSIndexPath indexPathForRow:i++ inSection:0]]; } [[self tableView] beginUpdates]; [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationNone]; [[self tableView] endUpdates]; [tempArray release]; } 
+13
source share

If you populate a table from an array, you can simply add an element to your array and call [myTable reloadData]

+2
source share

reloading the data is nice and simple, but does not know how much I can judge ...

+1
source share

ReloadData can add a line in the form of teble, but if you need animation when adding a line, you should use these lines of code.

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSMutableArray *tempArray = [[NSMutableArray alloc] init]; [tempArray addObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:0]]; [mainArray insertObject:@"new row" atIndex:indexPath.row+1]; [[self tableView] beginUpdates]; [[self tableView] insertRowsAtIndexPaths:(NSArray *)tempArray withRowAnimation:UITableViewRowAnimationFade]; [[self tableView] endUpdates]; } 
0
source share

All Articles