cell的編輯模式和側滑按鈕狀態總結

  • cell的編輯模式

self.tableView.allowsMultipleSelection = YES; //允許多選
self.tableView.allowsSelection = YES; //允許選擇某cell
self.tableView.allowsSelectionDuringEditing = YES; //編輯模式下允許可以
self.tableView.allowsMultipleSelectionDuringEditing = YES; //編輯模式下是否可以多選,選中后前面出現對勾
(如下圖)
self.tableView.indexPathForSelectedRow; // 被選中的行

cell多選.png
  • cell右滑插入左滑刪除

    //自定義cell的編輯模式
    - (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        if(){
           return UITableViewCellEditingStyleDelete; //在某行添加刪除功能
        }else{
           return UITableViewCellEditingStyleInsert; //在某行添加插入功能
        }
     }
    
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return YES; //設置為可編輯模式 tableview默認 editing為NO
    }
    
    //進入編輯模式,確定編輯提交操作時,執行的代理方法
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        //刪除行
        if (editingStyle == UITableViewCellEditingStyleDelete) {
           //一定先該行刪除數據
           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) {
            //可選擇插入的數據
            [_dataSourceArr insertObject:noticeItem atIndex:[_dataSourceArr objectAtIndex:indexPath.row]];
            //插入行
            [_tableView beginUpdates];
            [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
            num++;
            [_tableView endUpdates];
        }
    }
    
  • 移動cell位置

    //cell右邊出現編輯按鈕,長按可拖動
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
        //實現數據源的排序更換
        [_dataSourceArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    }
    
  • 如果想要左滑出現幾個自定義按鈕 (參考http://blog.csdn.net/lengshengren/article/details/44243473 ):

    - (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        //設置刪除按鈕
        UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
             [self.dataSource removeObjectAtIndex:indexPath.row];
             [self.tableView deleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
        }];
    
        //設置收藏按鈕
        UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) {
             collectRowAction.backgroundColor = [UIColor greenColor];
             NSLog(@"收藏成功");
        }];
    
        //設置置頂按鈕
        UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置頂"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) {
             //移動數據位次
             [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];
    }
    
  • 想要滑動出現的按鈕含文字和圖片,可以使用 MGSwipeTableCell
    參考: http://www.lxweimin.com/p/25908a61f849

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容