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.

4

4 回答 4

13

当我动态添加一行时,我使用 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];
}
于 2010-02-05T18:44:06.783 回答
2

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

于 2009-10-26T08:19:27.477 回答
1

重新加载数据既好又简单,但据我所知并没有动画......

于 2010-06-21T14:41:52.793 回答
0

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

}
于 2012-10-08T07:08:08.493 回答