- (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]; }