6

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

Am i suppose to use the following method???

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

What shoud i write inside the method to add a new row.?

Please do help me,

Thanks in advance Shibin.

JohnoBoy
  • 566
  • 1
  • 6
  • 26

4 Answers4

13

When I add a row 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];
}
Ryna
  • 984
  • 8
  • 7
2

If you are filling your table from an array, you can just add an element to your array and call [myTable reloadData]

Morion
  • 10,495
  • 1
  • 24
  • 33
  • no, i am not loading the new row data from an array. what i need is to add a new row when i select any particular row in the table. it just get appended to my existing table. –  Oct 26 '09 at 08:51
  • Hmm. And what is the source for your table view content? – Morion Oct 26 '09 at 09:53
  • 1
    You could just change the number of items returned by numberOfRowsInSection and update the data returned by cellForRowAtIndexPath appropriately. Then call reloadData as above. – Epsilon Prime Oct 28 '09 at 21:32
1

reload data is nice and simple, but does not animate as far as I can tell...

dhvidding
  • 21
  • 1
0

ReloadData can add row in teble view but if you want some animation when row add then 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];

}
Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43
Anand Mishra
  • 1,093
  • 12
  • 15