TableView的添加删除

步骤

  • 模型数据从NSArray->NSMutableArray
  • 在故事板(Storyboard)中添加 增加按钮 和 删除按钮

添加按钮

- (IBAction)add {
    // tableView里面需要显示新的cell数据,只需要操作模型数据
    XMGDeal *deal = [[XMGDeal alloc] init];
    deal.title = [NSString stringWithFormat:@"XX饭店大打折 %d折", arc4random_uniform(50)];
    deal.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
    deal.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
    deal.icon = @"5ee372ff039073317a49af5442748071";
    [self.deals insertObject:deal atIndex:0];
    
    XMGDeal *deal2 = [[XMGDeal alloc] init];
    deal2.title = [NSString stringWithFormat:@"YY饭店大打折 %d折", arc4random_uniform(50)];
    deal2.price = [NSString stringWithFormat:@"%d", 10 + arc4random_uniform(100)];
    deal2.buyCount = [NSString stringWithFormat:@"%d", arc4random_uniform(1000)];
    deal2.icon = @"5ee372ff039073317a49af5442748071";
    [self.deals insertObject:deal2 atIndex:0];
    
    // 提醒tabelView,模型数据发生了变化,请重新识别,请重新向数据源索要数据
    [self.tableView reloadData];
    // 插入某些特定的行
//    [self.tableView insertRowsAtIndexPaths:@[
//                                             [NSIndexPath indexPathForRow:0 inSection:0],
//                                             [NSIndexPath indexPathForRow:1 inSection:0]
//                                             ] withRowAnimation:UITableViewRowAnimationLeft];
}

删除按钮

- (IBAction)remove {
    // 移除模型数据
    [self.deals removeObjectAtIndex:0];
    [self.deals removeObjectAtIndex:0];
    [self.deals removeObjectAtIndex:0];
    
    // 刷新表格
    [self.tableView reloadData];
//    [self.tableView deleteRowsAtIndexPaths:@[
//                                             [NSIndexPath indexPathForRow:0 inSection:0],
//                                             [NSIndexPath indexPathForRow:1 inSection:0],
//                                             [NSIndexPath indexPathForRow:2 inSection:0]
//                                             ] withRowAnimation:UITableViewRowAnimationRight];
    
    // 15个cell:13
    // 15个模型:12
}

更新数据

- (IBAction)update {
//    XMGDealCell *cell = (XMGDealCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
//    cell.priceLabel.text = @"¥999";
//    
//    return;
    // 修改模型
    XMGDeal *deal = self.deals[3];
    deal.price = [NSString stringWithFormat:@"%d", 50 + arc4random_uniform(100)];;
    
    // 刷新表格
    [self.tableView reloadData];
//    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationMiddle];
}

进入编辑模式

- (IBAction)switchEditing {
    // 进入编辑模式
    //    self.tableView.editing = YES;
    [self.tableView setEditing:!self.tableView.isEditing animated:YES];
}

左划删除-编辑模式

#pragma mark - TableView代理方法
/**
 * 只要实现这个方法,左划cell出现删除按钮的功能就有了
 * 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {  // 点击了“删除”
        // 删除模型
        [self.deals removeObjectAtIndex:indexPath.row];
        
        // 刷新表格
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+
        NSLog(@"+++++ %zd", indexPath.row);
    }
}
/**
 * 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)
 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert: UITableViewCellEditingStyleDelete;
}

添加按钮弹出框

  • UIAlertView 弹出框方法
  • IOS8之后方法:UIAlertController
- (IBAction)add {
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"111" message:@"2222" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
//    
//    [alertView show];
//    UIActionSheet *sheet;
    
    // 创建弹框控制器
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入团购信息" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        // 创建模型
        XMGDeal *deal = [[XMGDeal alloc] init];
        deal.title = [alert.textFields[0] text];
        deal.price = [alert.textFields[1] text];
        [self.deals insertObject:deal atIndex:0];
        
        // 刷新数据
        [self.tableView reloadData];
    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"不知道" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"点击了不知道按钮");
//    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"不知道2" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        NSLog(@"点击了不知道2按钮");
//    }]];
    
    // 添加文本输入框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"请输入团购名字";
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"请输入团购价格";
    }];
    
    // 显示控制器
    [self presentViewController:alert animated:YES completion:nil];
}

批量操作

  • 步骤
  • 给check选中按钮添加到模型数据中
    @property (assign, nonatomic, getter=isCheck) BOOL check;
  • 连接输出接口,并设置默认隐藏
    @property (weak, nonatomic) IBOutlet UIImageView *check;
    self.check.hidden = !tg.isCheck;
  • 调用代理方法
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// 按钮方法
- (IBAction)remove {
    // 临时数组:存放即将需要删除的团购数据
    NSMutableArray *deletedTgs = [NSMutableArray array];
    for (YFtg *tg in self.tgs) {
        if (tg.isCheck) [deletedTgs addObject:tg];
    }
    // 删除模型数据
    [self.tgs removeObjectsInArray:deletedTgs];
    [self.tableView reloadData];
}

// 代理方法:选中被删除方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 点击不被选中
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    // 默认隐藏,所以取反 打钩
    YFtg *tg = self.tgs[indexPath.row];
    tg.check = !tg.isCheck;
    // 更新数据
    [self.tableView reloadData];
}

你可能感兴趣的:(TableView的添加删除)