首先创建项目,在storyboard如下布局控件,设置好约束
然后创建cell模型类XMGWineCell
数据模型类XMGWine
创建UITableView,设置数据源协议,实现数据源方法
懒加载数据
这些在前面已经做过很多次了,代码就不一一写了
一、全局刷新
1、添加单组数据并全局刷新
-(IBAction)add {//添加模型数据
XMGWine *wine =[[XMGWine alloc] init];
wine.money= @"20.5";
wine.name= @"很好喝的酒";
wine.image= @"new_wine";
[self.wineArray insertObject:wine atIndex:0];//告诉tableView:模型数据改变了,赶紧刷新表格
[self.tableView reloadData];
}
2、删除单组数据并全局刷新
-(IBAction)remove {//删除模型数据
[self.wineArray removeObjectAtIndex:0];
[self.wineArray removeObjectAtIndex:0];//告诉tableView:模型数据改变了,赶紧刷新表格
[self.tableView reloadData];
}
3、更改数据并全局刷新
-(IBAction)update {//更改模型数据
XMGWine *wine = self.wineArray[0];
wine.money= @"100";
XMGWine*wine2 = self.wineArray[1];
wine2.name= @"哈哈";//告诉tableView:模型数据改变了,赶紧刷新表格
[self.tableView reloadData];
}
二、局部刷新
1、添加多组数据并局部刷新
-(IBAction)add {//添加模型数据
XMGWine *wine =[[XMGWine alloc] init];
wine.money= @"20.5";
wine.name= @"很好喝的酒";
wine.image= @"new_wine";
[self.wineArray insertObject:wine atIndex:0];
XMGWine*wine2 =[[XMGWine alloc] init];
wine2.money= @"100.5";
wine2.name= @"很好";
wine2.image= @"new_wine";
[self.wineArray insertObject:wine2 atIndex:0];//刷新
NSArray *indexPaths =@[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
}
2、删除多组数据并局部刷新
-(IBAction)remove {//删除模型数据
[self.wineArray removeObjectAtIndex:0];
[self.wineArray removeObjectAtIndex:0];//刷新
NSArray *indexPaths =@[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]
];
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationMiddle];
}
3、修改多项数据并局部刷新
-(IBAction)update {//更改模型数据
XMGWine *wine = self.wineArray[0];
wine.money= @"100";
XMGWine*wine2 = self.wineArray[2];
wine2.image= @"new_wine";
XMGWine*wine3 = self.wineArray[3];
wine3.image= @"new_wine";//局部刷新
NSArray *indexPaths =@[
[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0]
];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
}
三、左滑操作
先设置代理
1、只要实现了这个方法,左滑出现Delete按钮的功能就有了
点击了“左滑出现的Delete按钮”会调用这个方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{//删除模型
[self.wineArray removeObjectAtIndex:indexPath.row];//刷新
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
2、修改Delete按钮的文字
/**
* 修改Delete按钮文字为“删除”*/
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{return @"删除";
}
四、左滑出现更多按钮
/**
* 左滑cell时出现什么按钮*/
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction*action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"关注" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了关注");//收回左滑出现的按钮(退出编辑模式)
tableView.editing =NO;
}];
UITableViewRowAction*action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
[self.wineArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}];return@[action1, action0];
}
五、进入编辑模式
我们先将删除按钮改一改
-(IBAction)remove {//(上面的没动画效果,下面有动画效果)//self.tableView.editing = !self.tableView.isEditing;
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
点击按钮之后左边会出现红色圆点,点击红点的效果和左滑是一样的,都可以进入编辑模式
六、批量删除
重新设置按钮
//批量删除按钮
-(IBAction)multipleRemove {
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
self.removeButton.hidden= !self.tableView.isEditing;
}-(IBAction)remove {//self.tableView.indexPathsForSelectedRows = [0, 1]//获得需要删除的酒模型数据
NSMutableArray *deletedWineArray =[NSMutableArray array];for (NSIndexPath *indexPath inself.tableView.indexPathsForSelectedRows) {
[deletedWineArray addObject:self.wineArray[indexPath.row]];
}//删除模型数据
[self.wineArray removeObjectsInArray:deletedWineArray];//刷新
[self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationLeft];
}
并且在viewDidLoad中设置
//编辑模式的时候可以多选
self.tableView.allowsMultipleSelectionDuringEditing =YES;//隐藏删除按钮
self.removeButton.hidden = YES;
以上代码就能实现系统自带的批量删除
七、自定义批量删除
功能:在选中后添加一个图片来标记,所有标记的都可以批量删除
1、首先在数据模型XMGWine中添加属性checked
/** 是否被勾选*/@property (nonatomic, assign, getter=isChecked) BOOL checked;
2、实现代理方法,判断打钩控件显示还是隐藏
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//修改模型
XMGWine *wine =self.wineArray[indexPath.row];
wine.checked = !wine.isChecked;//刷新
[tableView reloadData];
}
注意:核心思想 "修改数据模型从而达到修改cell的目的"
如果直接在cell上添加一个图片来标记选定与否,那么在cell复用时会出现问题,你会发现往下滑 下面的有些cell也有图片标记,记住,一般MVC模式中都是修改数据
那么在这里我们已经修改了wine.checked的值,下一步在cellForRowAtIndexPath方法中赋值时(也就是说在要显示cell之前),在- (void)setWine:(XMGWine *)wine方法中实现下面这一句
//根据模型的checked属性决定打钩控件显示还是隐藏
self.checkImageView.hidden = !wine.isChecked;
3、删除所有被打钩的模型
-(IBAction)remove {//获得所有被打钩的模型
NSMutableArray *deletedWineArray =[NSMutableArray array];for (XMGWine *wine inself.wineArray) {if(wine.isChecked) {
[deletedWineArray addObject:wine];
}
}//删除所有被打钩的模型
[self.wineArray removeObjectsInArray:deletedWineArray];//刷新表格
[self.tableView reloadData];
}