2009-06-09 105 views
79

我想要類似於報警應用程序的東西,在其中您不能輕掃刪除行,但仍可以在編輯模式中刪除該行。UITableView禁用滑動刪除,但仍然在編輯模式下刪除?

當註釋掉tableView:commitEditingStyle:forRowAtIndexPath時,我禁用了滑動刪除功能,並且在編輯模式下仍然有刪除按鈕,但是當按下刪除按鈕時會發生什麼情況。什麼被稱爲?

回答

258

好的,事實證明這很容易。這是我做過什麼來解決這個問題:

Objective-C的

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Detemine if it's in editing mode 
    if (self.tableView.editing) 
    { 
     return UITableViewCellEditingStyleDelete; 
    } 

    return UITableViewCellEditingStyleNone; 
} 

斯威夫特2

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { 
    if tableView.editing { 
     return .Delete 
    } 

    return .None 
} 

斯威夫特3

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 
    if tableView.isEditing { 
     return .delete 
    } 

    return .none 
} 

您仍需要執行tableView:commitEditingStyle:forRowAtIndexPath:才能提交刪除。

+0

bu然後再次滑動即可刪除。或不? – 2009-06-09 15:41:55

1

基本上,您可以啓用或使用方法

- (void)setEditing:(BOOL)editing animated:(BOOL)animated 

如果啓用編輯,出現紅色刪除圖標,並要求用戶刪除的構象禁用編輯。如果用戶確認,則代理方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

被通知刪除請求。如果您實施此方法,則輕掃以刪除將自動激活。如果你沒有實現這個方法,那麼滑動刪除不是主動的,但是你不能真正刪除該行。因此,據我所知,除非使用一些未公開的私有API,否則無法實現所要求的內容。可能這就是Apple應用程序的實現方式。

+1

我解決了這個由迴歸UITableViewCellEditingStyleDelete中的tableView:editingStyleForRowAtIndexPath:如果它在編輯模式。 – willi 2009-06-09 15:30:37

8

爲了清楚起見,除非實施tableView:commitEditingStyle:forRowAtIndexPath:,否則不會啓用輕掃即刪除功能。

在開發過程中,我沒有實現它,因此沒有啓用滑動刪除功能。當然,在完成的應用程序中,它將始終得到實施,否則將不會進行編輯。

3

斯威夫特版本:

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { 

    if(do something){ 

     return UITableViewCellEditingStyle.Delete or UITableViewCellEditingStyle.Insert 

    } 
    return UITableViewCellEditingStyle.None 

} 
相關問題