做个记录,免得忘记 数据源初始化过程略 #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Hero *hero = self.heros[indexPath.row]; // 从数据源获取数据 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"修改数据" message:@"新的名称" preferredStyle:UIAlertControllerStyleAlert]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.keyboardType = UIKeyboardTypeNumberPad; textField.placeholder = @"输入新的名称"; textField.text = hero.name; }]; UIAlertAction *canselAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *textField = [[alertController textFields] firstObject]; if (textField.text.length == 0) { NSLog(@"名称不能为空"); }else{ // 修改数据源 hero.name = textField.text; // 刷新整个数据 //[tableView reloadData]; // 刷新指定行数据 NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; [tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight]; } }]; [alertController addAction:canselAction]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }
发表评论