简体   繁体   English

在iphone的TableView中动态添加一行

[英]Add a row dynamically in TableView of iphone

I need to add a new row in the tableview when i select a row in the table. 当我在表格中选择一行时,我需要在tableview中添加一个新行。

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. 提前谢谢Shibin。

When I add a row dynamically, I use insertRowsAtIndexPaths: here is an example function 当我动态添加一行时,我使用insertRowsAtIndexPaths:这是一个示例函数

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

如果从数组中填充表,只需向数组中添加一个元素并调用[myTable reloadData]

重新加载数据很简单,但据我所知,它没有动画...

ReloadData can add row in teble view but if you want some animation when row add then you should use these lines of code. ReloadData可以在teble视图中添加行,但如果你想在行添加时想要一些动画,那么你应该使用这些代码行。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM