---------------"编辑"模式: 更改名字
// 弹出alerView
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"编辑"
message:@""
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"编辑"
message:@""
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"确定", nil];
// 设置alertView的 style
/**
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput, 安全输入
UIAlertViewStylePlainTextInput, 文本输入
UIAlertViewStyleLoginAndPasswordInput 登录
*/
/**
UIAlertViewStyleDefault = 0,
UIAlertViewStyleSecureTextInput, 安全输入
UIAlertViewStylePlainTextInput, 文本输入
UIAlertViewStyleLoginAndPasswordInput 登录
*/
alerView.alertViewStyle = UIAlertViewStylePlainTextInput;// 文本输入
// 1. 获取当前被点击cell 所对应的 模型
HeroModel *model = self.dataArray[indexPath.row];
// 2. 把英雄的名字, 显示到 textField 中
// 获取 alertView 中 指定位置 的 textFiled
UITextField *textFiled = [alerView textFieldAtIndex:0];
textFiled.text = model.name;
// 设置当前aletView 的 tag 为 indexPath.row
alerView.tag = indexPath.row;
[alerView show];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) { // 点击了确定按钮
// 1. 获取alertView 中 输入框中的内容
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *newName = textField.text;
// 2. 刷新数据
// 2.1 修改数据源中 被点击cell 对应的 模型对象中 name 修改为 newName
// 先取出 alertView 的 tag
NSInteger index = alertView.tag;
// 取出index 对应到数组中的元素
HeroModel *model = self.dataArray[index];
model.name = newName;
// 2.2 刷新数据
// 这个刷新所有数据
// [_tableView reloadData];
// 刷新指定cell 数据
// 创建NSIndexPath
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
/**
AtIndexPaths: 传递 一个数组 数组中包含 NSIndexPath
withRowAnimation : 动画效果
*/
[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
if (buttonIndex == 1) { // 点击了确定按钮
// 1. 获取alertView 中 输入框中的内容
UITextField *textField = [alertView textFieldAtIndex:0];
NSString *newName = textField.text;
// 2. 刷新数据
// 2.1 修改数据源中 被点击cell 对应的 模型对象中 name 修改为 newName
// 先取出 alertView 的 tag
NSInteger index = alertView.tag;
// 取出index 对应到数组中的元素
HeroModel *model = self.dataArray[index];
model.name = newName;
// 2.2 刷新数据
// 这个刷新所有数据
// [_tableView reloadData];
// 刷新指定cell 数据
// 创建NSIndexPath
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
/**
AtIndexPaths: 传递 一个数组 数组中包含 NSIndexPath
withRowAnimation : 动画效果
*/
[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
---------------"编辑"模式: 更改名字
pragma mark - 点击cell的时候调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 0. 取出当前cell对应的model
HeroModel *model = self.dataArray[indexPath.row];
// 使用alertController
// 1. 实例化一个alertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Edit" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 2. 添加文本输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.text = model.name;
}];
// 3. 添加action
// 3. 添加 cacel action
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
// 添加 cancel action 到 alertControler
[alertController addAction:cancelAction];
// 4. 添加sureAction
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 获取到修改后的文本
UITextField *field = [alertController textFields].lastObject;
NSString *newName = field.text;
// 修改数据源中的 模型 对象中的 name属性
model.name = newName;
// 刷新数据
[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}];
// sureAction 添加到控制器
[alertController addAction:sureAction];
// 展示alertCOntroller
[self presentViewController:alertController animated:YES completion:nil];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// 0. 取出当前cell对应的model
HeroModel *model = self.dataArray[indexPath.row];
// 使用alertController
// 1. 实例化一个alertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Edit" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 2. 添加文本输入框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.text = model.name;
}];
// 3. 添加action
// 3. 添加 cacel action
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
// 添加 cancel action 到 alertControler
[alertController addAction:cancelAction];
// 4. 添加sureAction
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
// 获取到修改后的文本
UITextField *field = [alertController textFields].lastObject;
NSString *newName = field.text;
// 修改数据源中的 模型 对象中的 name属性
model.name = newName;
// 刷新数据
[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}];
// sureAction 添加到控制器
[alertController addAction:sureAction];
// 展示alertCOntroller
[self presentViewController:alertController animated:YES completion:nil];
}
// 隐藏状态栏
- (BOOL)prefersStatusBarHidden {
return YES;
}
- (BOOL)prefersStatusBarHidden {
return YES;
}