Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add rows to existing UITableView section

I am trying to get some sample code on how I would add rows to an existing UITableView. I am trying to use the insertRowsAtIndexPaths: function.

    [tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop];

Any ideas how these indexes work and how I can add to an existing section or, if I don't have a section, then create a new section?

like image 489
Scoota P Avatar asked May 06 '11 04:05

Scoota P


1 Answers

You have to create an array of indexpaths as -

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

for example if you want to insert 2nd row in section 0 and already have 1 row in section 0 then create an index path for row 1 in section 0 and call the insertRowsAtIndexPaths on table view, it will insert 2nd row in section.

If there is no section in table view then you should use an int for data source to give no of sections in table view, in this use -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return noOfSection; //here section is as int which is initially zero
}

and initially your int "noOfSection" will be zero then there will be no section in table, then when you want to add section increase your int value by 1 and call [tableView reloadData];

like image 140
saadnib Avatar answered Sep 30 '22 12:09

saadnib