您的位置:首页 > 移动开发 > IOS开发

iOS边练边学--简单的数据操作(增、删、改),左滑动删除和弹窗

2016-03-18 14:46 666 查看
一、数据刷新的原则:

通过修改模型数据,来修改tableView的展示

先修改数据模型

在调用数据刷新方法

不要直接修改cell上面子控件的属性

二、增删改用到的方法:

  <1>重新绑定屏幕上所有的cell,这个方法没有动画效果,但是以下三种方法通过这个方法都可以办到

// 重新加载数据,刷新的是整个页面,没有动画
[self.tableView reloadData];


  <2>刷新特定的cell,可以设置动画效果

// 刷新指定的cell
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];


  <3>插入特定行数的cell,可以设置动画效果

// 只是刷新添加的数据,可以同时设置动画
[self.tableView insertRowsAtIndexPaths:@[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
] withRowAnimation:UITableViewRowAnimationMiddle];


  <4>删除特定行数的cell,可以设置动画效果

[self.dealArray removeObjectAtIndex:0];
[self.tableView deleteRowsAtIndexPaths:@[
[NSIndexPath indexPathForRow:0 inSection:0]
] withRowAnimation:UITableViewRowAnimationFade];


三、

  <1>左滑动删除效果,需要实现tableView的代理方法。实现该方法后默认实现的是左滑动有删除按钮,但是这个方法会处理两种情况:删除和添加

#pragma mark - tableView代理方法
// 只要实现了这个方法,左滑cell就会出现删除按钮
// 调用时机:用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleInsert) { // 添加

} else { // 删除

[self.dealArray removeObjectAtIndex:indexPath.row];
//    [self.tableView reloadData];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}




  <2>配合下面这个方法,可以决定编辑的类型,前提是self.tableView.editing = YES;

// 这个方法决定了编辑模式时,每一行的编辑类型:inset(+按钮) delete(—按钮)  不实现这个方法默认返回的是delete
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return indexPath.row % 2 ? UITableViewCellEditingStyleInsert:UITableViewCellEditingStyleDelete;
}




四、创建弹框控制器iOS8增加的新功能,其中屏幕下方的弹窗中不能添加文本框,否则会报错

// 创建弹框控制器,弹框有两种,一种是在中间的一种,另一种是从最下面往上展现的一种
// UIAlertControllerStyleAlert(中间的弹窗)
// UIAlertControllerStyleActionSheet(底端的弹窗)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请输入团购信息" message:nil preferredStyle:UIAlertControllerStyleAlert];

// 添加按钮 最后的block参数是点击按钮后执行的代码,取消按钮中的block可以不用设置
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
// 在点击确定按钮的block中做相应的操作
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
// 创建模型
XMGDeal *deal = [[XMGDeal alloc] init];
// 可以通过alert弹窗中的textFields集合属性获得弹窗中对应文本框的值
deal.title = [alert.textFields[0] text];
deal.price = [alert.textFields[1] text];
[self.deals insertObject:deal atIndex:0];

// 刷新数据
[self.tableView reloadData];
}]];

// 添加文本输入框
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
// 设置占位字符,对于用户有一定的提示作用
textField.placeholder = @"请输入团购名字";
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"请输入团购价格";
}];

// 将弹窗展现出来动画效果,显示控制器
[self presentViewController:alert animated:YES completion:nil];



    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: