- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{ return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{ return UITableViewCellEditingStyleDelete;
}
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *layTopRowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了删除");
[tableView setEditing:NO animated:YES];
}];
layTopRowAction1.backgroundColor = [UIColor redColor];
UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了置顶");
[tableView setEditing:NO animated:YES];
}];
layTopRowAction2.backgroundColor = [UIColor greenColor];
UITableViewRowAction *layTopRowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@"点击了更多");
[tableView setEditing:NO animated:YES];
}];
layTopRowAction3.backgroundColor = [UIColor blueColor];
NSArray *arr = @[layTopRowAction1,layTopRowAction2,layTopRowAction3];
return arr;
}
//设置滑动时显示多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//添加一个删除按钮
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@删除 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@点击了删除);
//1.更新数据
[self.dataArray removeObjectAtIndex:indexPath.row];
//2.更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}];
//删除按钮颜色
deleteAction.backgroundColor = [UIColor cyanColor];
//添加一个置顶按钮
UITableViewRowAction *topRowAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@置顶 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@点击了置顶);
//1.更新数据
[self.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
//2.更新UI
NSIndexPath *firstIndexPath =[NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
//置顶按钮颜色
topRowAction.backgroundColor = [UIColor magentaColor];
//--------更多
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@更多 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES];
}];
//背景特效
//moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
//----------收藏
UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@收藏handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@收藏 message:@收藏成功 delegate:self cancelButtonTitle:@确定 otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
}];
//收藏按钮颜色
collectRowAction.backgroundColor = [UIColor greenColor];
//将设置好的按钮方到数组中返回
return @[deleteAction,topRowAction,moreRowAction,collectRowAction];
// return @[deleteAction,topRowAction,collectRowAction];
}