目前需要實現一個功能:
- UITableViewCell
單選刪除
與多選刪除
內容具體要求是:- 在 沒有 點擊 多選刪除 的時候只能進行 單選刪除
- 點擊了多選刪除, 就只能多選刪除, 并禁止單選刪除操作
- 也就是說,
單選刪除
與多選刪除
,不能同時出現
下面開始上關鍵地方的代碼
- 單選刪除
// 重寫該方法,就會出現刪除按鈕
- (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; 這個方法...