iOS開發學習之#表視圖#(2)添加行 – iPhone手機開發技術文章 iPhone軟體開發教學課程

繼續上篇學到的刪除行,有刪除就有添加:添加行我們用

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

其中(NSArray *)indexPaths用於識別表視圖中得行,(UITableViewRowAnimation)animation用來指定動畫

核心代碼如下:

//設置表單元的編輯風格
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    returnUITableViewCellEditingStyleInsert;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    //判斷表單元的編輯風格
    if (editingStyle ==UITableViewCellEditingStyleDelete) {
        [aremoveObjectAtIndex:indexPath.row];
        [tvdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    elseif (editingStyle == UITableViewCellEditingStyleInsert){
        i =i+1;
        NSInteger row = [indexPathrow];
        NSArray *insert = [NSArrayarrayWithObjects:indexPath, nil];
        NSString *str = [NSStringstringWithFormat:@"%i",i];
        [ainsertObject:str atIndex:row];
        [tvinsertRowsAtIndexPaths:insert withRowAnimation:UITableViewRowAnimationRight];
        
    }
}

和上篇一樣要遵守的協議:

@interface ViewController :UIViewController{
    IBOutletUITableView *tv;
    NSMutableArray *a;
    NSArray *b;
    int i;
}

發佈留言