2010-09-05 70 views
7

設置我的viewController在蘋果建議的方式來使用編輯按鈕編輯按鈕:刷卡刪除與拍了拍iPhone

self.navigationItem.rightBarButtonItem = self.editButtonItem; 

當用戶點擊編輯按鈕,它會導致setEditing:animated:方法觸發。在這種方法中,我根據editing值(即用戶可以點擊添加新項目的行)添加或刪除「新行」。

當用戶在單元格上滑動(而不是在編輯模式下)時,它也會調用setEditing:animated:,這會導致我的代碼錯誤地添加此「新行」。當整個viewController處於編輯模式時(即點擊編輯按鈕時),它應該只顯示這個新行。

我該如何解決這個問題?

回答

12

如果實現tableView:willBeginEditingRowAtIndexPath:tableView:didEndEditingRowAtIndexPath:,方法調用將改變的行爲......

它將不再稱setEditing:animated:,你必須處理所有編輯變化邏輯上述兩種方法中。作爲此的副作用,當調用滑動手勢時,self.editing將不再爲YES。這是因爲setEditing:animated:負責設置self.editing = YES

+0

你能改變刪除的圖像嗎? – bluezald 2011-09-10 03:56:24

0

我不知道你的「新行」是關於什麼的,但是你可以將你的編輯按鈕附加到一個輔助方法,該方法在調用setEditing之前設置一個標誌,並且你可以檢查是否設置了該標誌並相應地執行。然後在方法結束時清除標誌。

+0

沒有重寫編輯按鈕的行爲沒有辦法做到這一點?如果可能,我想使用Apple的建議。 (我澄清了這個問題) – Senseful 2010-09-05 18:09:39

+0

反正加入新的行標準行爲?我認爲一個「+」或「添加」按鈕將是執行此操作的一致方式。如果用戶需要按編輯添加新項目,則需要大量支持郵件。 :-) – Eiko 2010-09-05 18:31:37

+0

查看聯繫人應用程序添加電子郵件/電話 – Senseful 2010-09-05 19:08:37

0

有意思的你必須重寫setEditing:animated:method。甚至你可以像Eiko所建議的那樣在這個方法中設置標誌。

或另一種方式是

ü實現自定義naviagtion吧。在這種情況下,你需要導航欄的圖像和它上面的2個按鈕。 將目標添加到該按鈕並根據需要實現功能

3

使用UITableViewDelegate方法tableView:willBeginEditingRowAtIndexPath:。從文檔:

"This method is called when the user swipes horizontally across a row; as a 
consequence, the table view sets its editing property to YES (thereby entering editing 
mode) and displays a Delete button in the row identified by indexPath. In this "swipe to 
delete" mode the table view does not display any insertion, deletion, and reordering 
controls. This method gives the delegate an opportunity to adjust the application's user 
interface to editing mode. When the table exits editing mode (for example, the user taps 
the Delete button), the table view calls tableView:didEndEditingRowAtIndexPath:." 

tableView:willBeginEditingRowAtIndexPath:,設置編輯模式是使用滑動刪除觸發的標誌。然後,在setEditing:animated:中,檢查該標誌以查看編輯模式是否正常觸發或使用滑動刪除並根據該檢查執行某些操作。最後,重置tableView:didEndEditingRowAtIndexPath:中的標誌,以便在按下編輯按鈕時執行默認操作。