2010-05-29 139 views
4

我有一個表格視圖,並希望允許重新排序所有單元格,但是有某些單元格我不希望被允許刪除。當UiTableView進入刪除模式時,我不想讓紅色的' - '按鈕出現在左側,並且不想讓滑動手勢調出這些單元格的刪除按鈕,但希望它爲其他人發生。有任何想法嗎?UITableViewCell:允許選擇性刪除

回答

7
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    //if we cant delete the object represented at the index path 
    if ([[tableViewObjectsArray objectAtIndex:indexPath.row] canBeDeleted] == NO){ 
     return UITableViewCellEditingStyleNone; 
    } 
    //otherwise allow the deletion 
    else{ 
     return UITableViewCellEditingStyleDelete; 
    } 
} 

當然這留下一個空的空間,其中「 - 」按鈕應該的,但它不允許刪除。並且也不允許滑動刪除。

+0

實現' - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath'來防止縮進,如果你不想那:) – Johanneke 2013-11-13 16:42:57

2

實現:

// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
+0

這將停止單元格能夠重新排序,我只想停止刪除並允許重新排序。 – 2010-05-29 11:40:50