- cell的編輯模式
self.tableView.allowsMultipleSelection = YES; //允許多選
self.tableView.allowsSelection = YES; //允許選擇某cell
self.tableView.allowsSelectionDuringEditing = YES; //編輯模式下允許可以
self.tableView.allowsMultipleSelectionDuringEditing = YES; //編輯模式下是否可以多選,選中后前面出現(xiàn)對(duì)勾
(如下圖)
self.tableView.indexPathForSelectedRow; // 被選中的行
-
cell右滑插入左滑刪除
//自定義cell的編輯模式 - (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { if(){ return UITableViewCellEditingStyleDelete; //在某行添加刪除功能 }else{ return UITableViewCellEditingStyleInsert; //在某行添加插入功能 } } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; //設(shè)置為可編輯模式 tableview默認(rèn) editing為NO } //進(jìn)入編輯模式眨业,確定編輯提交操作時(shí)絮姆,執(zhí)行的代理方法 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { //刪除行 if (editingStyle == UITableViewCellEditingStyleDelete) { //一定先該行刪除數(shù)據(jù) NoticeItem *noticeItem = [_dataSourceArr objectAtIndex:indexPath.row]; [_http deleteNotificationAction:noticeItem.noticeId]; [_dataSourceArr removeObject:noticeItem]; //再去刪除此行 dispatch_async(dispatch_get_main_queue(), ^{ [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:deleteIndexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade]; }); } //插入行 if(editingStyle == UITableViewCellEditingStyleInsert) { //可選擇插入的數(shù)據(jù) [_dataSourceArr insertObject:noticeItem atIndex:[_dataSourceArr objectAtIndex:indexPath.row]]; //插入行 [_tableView beginUpdates]; [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight]; num++; [_tableView endUpdates]; } }
-
移動(dòng)cell位置
//cell右邊出現(xiàn)編輯按鈕,長(zhǎng)按可拖動(dòng) - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { //實(shí)現(xiàn)數(shù)據(jù)源的排序更換 [_dataSourceArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row]; }
-
如果想要左滑出現(xiàn)幾個(gè)自定義按鈕 (參考http://blog.csdn.net/lengshengren/article/details/44243473 ):
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { //設(shè)置刪除按鈕 UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) { [self.dataSource removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; }]; //設(shè)置收藏按鈕 UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) { collectRowAction.backgroundColor = [UIColor greenColor]; NSLog(@"收藏成功"); }]; //設(shè)置置頂按鈕 UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) { //移動(dòng)數(shù)據(jù)位次 [self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0]; //改變行的位置 NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section]; [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath]; }]; collectRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; topRowAction.backgroundColor = [UIColor blueColor]; collectRowAction.backgroundColor = [UIColor grayColor]; return @[deleteRowAction,collectRowAction,topRowAction]; }
想要滑動(dòng)出現(xiàn)的按鈕含文字和圖片,可以使用 MGSwipeTableCell
參考: http://www.reibang.com/p/25908a61f849