iOS开发之tableView的数据添加删除更新操做

    在tableView中进行数据的添加删除更新操做,其实就是对模型数据就行修改,而后再让tableView从新load一遍数据的过程,主要是一些api的认识。
api

一、添加行:数组

- (IBAction)add {
    
    XXModel *model = [[XXModel alloc]init];
    //设置model的属性数据
    //...
    //添加在最前面的一行
    [self.models insertObject:model atIndex:0];
    
    //通知tableView数据源发生了改变,须要从新加载数据
    //[self.tableView reloadData];
    
    //少行操做时,insertRowsAtIndexPaths效率更高
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    
    //也能够一次添加多行,可是添加的对象个数必定要和NSIndexPath数组对应
    [self.tableView insertRowsAtIndexPaths:@[
                            [NSIndexPath indexPathForRow:0 inSection:0]
                            [NSIndexPath indexPathForRow:1 inSection:0]
                            ] withRowAnimation:UITableViewRowAnimationTop];
}

二、删除行:微信

- (IBAction)romove {
    
    [self.deals removeObjectAtIndex:0];
    
//    [self.tableView reloadData];
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    
}

三、更新某行数据:spa

- (IBAction)update {
    
    XXModel *model = self.models[3];
    //修改model数据
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    
}


相似微信那种在tableView中某行左滑显示删除按钮的功能api:
代理

#pragma mark -tableView代理方法
/**
 * 只要实现这个方法,左划cell出现删除按钮的功能就有了
 * 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操做时会调用
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //editingStyle的值默认是UITableViewCellEditingStyleDelete
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        //添加操做
    }else if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除操做
    }
}

另外还有tableView的编辑模式,设置编辑模式:code

// 进入编辑模式
[self.tableView setEditing:!self.tableView.isEditing animated:YES];

// 容许在编辑模式进行多选操做,用做批量操做
self.tableView.allowsMultipleSelectionDuringEditing = YES;
/**
 * 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.row % 2  == 0 ? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}


若是要想左滑弹出多个按钮,在iOS8.0以后能够经过这个方法实现:对象

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"置顶");
    }];
    action.backgroundColor = [UIColor orangeColor];
    
    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"订阅" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"订阅");
    }];
    action1.backgroundColor = [UIColor grayColor];
    
    UITableViewRowAction *action2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"删除");
    }];
    
    return @[action,action1,action2];
    
}

效果图:ip

相关文章
相关标签/搜索