OC:UITableView选中行,弹出弹窗,在弹窗内修改该行内容

555 阅读1分钟
//监听行被选中的代理方法
 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //创建一个对话框
    //获取名字
    Group * group = self.groups[indexPath.section];
    Car * car = group.cars[indexPath.row];
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"你的车" message:@"你的车名字" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
 
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
          textField.placeholder = @"请输入车名";
          textField.keyboardType = UIKeyboardTypeDefault;
      }];
    UIAlertAction * conform = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *envirnmentNameTextField = alert.textFields.firstObject;
        car.name = envirnmentNameTextField.text;
        //重新刷新整个tableView
        [tableView reloadData];
        
        //局部刷新
        //刷新指定的行
//该方法只能在行数没有变动的情况下使用

//        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }];
    
    [alert addAction:cancel];
    [alert addAction:conform];
    [self presentViewController:alert animated:YES completion:nil];
    
}

效果

image.png

image.png

局部刷新(指定行刷新):把改行单元格删除,再重新创建一个单元格插在原来的位置上

所以,有一个删除,对应一个新建,如果遇到刷新增减单元格的状况则无法使用局部刷新。