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