添加行动态的iphone的TableView中(Add a row dynamically in T

2019-09-01 05:14发布

我需要添加在tableview中新行的时候我选择表中的一行。

是我该使用下面的方法???

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

768,16我写的东西添加一个新行的方法里面?

请你帮帮我,

在此先感谢世斌。

Answer 1:

当我动态地添加行,我用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];
}


Answer 2:

如果您是从一个数组填充你的表,你可以将元素添加到您的数组,并调用[myTable reloadData]



Answer 3:

重装数据是好的,简单的,但据我所知没有动画...



Answer 4:

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

}


文章来源: Add a row dynamically in TableView of iphone