Add a row dynamically in TableView of iphone

2019-02-13 16:57发布

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.

4条回答
来,给爷笑一个
2楼-- · 2019-02-13 17:18

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

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-13 17:19

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];
}
查看更多
\"骚年 ilove
4楼-- · 2019-02-13 17:41

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

查看更多
5楼-- · 2019-02-13 17:41

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];

}
查看更多
登录 后发表回答