UITableViewCell 刪除 自定義刪除與系統刪除共存

目前需要實現一個功能:

  • UITableViewCell 單選刪除多選刪除
    內容具體要求是:
    1. 沒有 點擊 多選刪除 的時候只能進行 單選刪除
    2. 點擊了多選刪除, 就只能多選刪除, 并禁止單選刪除操作
    3. 也就是說, 單選刪除多選刪除,不能同時出現

下面開始上關鍵地方的代碼
  • 單選刪除

// 重寫該方法,就會出現刪除按鈕
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 刪除操作的代碼塊...
}

// 修改刪除按鈕的名稱
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"刪除";
}
  • 多選刪除

// 點擊當前cell響應事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 添加按鈕沒有隱藏則為單選刪除
    if (self.addButton.hidden) {
        LDCellModel *c = self.array[indexPath.row];
        c.check = !c.check;
        [self.tableView reloadData];   // 刷新
    }
}
// 返回特定表格上的編輯樣式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.addButton.hidden) {
        return UITableViewCellEditingStyleNone;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}


實現該功能最關鍵的方式就是在于

-(UITableViewCellEditingStyle)tableView:(UITableView)tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath; 這個方法...

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

推薦閱讀更多精彩內容