2017-02-13 100 views
0

作爲默認行爲,一旦您想要刪除任何單個表格視圖的單元格,您將點擊cel左側的刪除按鈕,刪除確認按鈕將顯示在單元格右側,然後繼續點擊此按鈕,該行將被刪除。對於這種行爲,您需要有2個步驟來刪除一行。是否有任何方法只能點擊刪除按鈕(在單元格左側)刪除單元格而不點擊確認按鈕?UITableView自定義刪除按鈕功能

+0

其中左側的按鈕?請截圖。 – Vahid

+0

您可以在表格單元格上添加一個按鈕,並在其上添加刪除圖標圖像,然後創建自定義方法以刪除表格行。 –

+0

這是我腦海中的解決方案。但我不確定這將是最好的方法,並期待更好的解決方案。 –

回答

0

你的意思是隻用左對齊來刪除行嗎?忽略刪除按鈕?

你可以使用UISwipeGestureRecognizer,像這樣:

class YourViewController: UITableViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var swipe = UISwipeGestureRecognizer(target: self, action: #selector(self.didSwipe)) 
     self.tableView.addGestureRecognizer(swipe) 
    } 

    func didSwipe(recognizer: UIGestureRecognizer) { 

     if swipe.state == UIGestureRecognizerState.Ended { 

      let swipeLocation = swipe.locationInView(self.tableView) 

      if let swipedIndexPath = tableView.indexPathForRowAtPoint(swipeLocation) { 

       if let swipedCell = self.tableView.cellForRowAtIndexPath(swipedIndexPath) { 

        self.cellObjectsArray.remove(at: swipedIndexPath.row) 
        tableView.deleteRows(at: [swipedIndexPath], with: .fade) 
       } 
      } 
     } 
    } 

} 
+0

問題是問,**有什麼辦法只能點擊左邊的按鈕來刪除單元格而不用點擊確認按鈕?**不會_僅通過交換刪除左邊的行0 –

+0

對不起,回覆遲了。我想要在單元格左邊的刪除按鈕上只需點擊一下鼠標的刪除功能,無需繼續點擊單元右側的確認按鈕。作爲默認行爲,您必須有2個步驟才能刪除行。 –

+0

如果是這樣的話,我就讓Vahid的問題成爲我自己的問題:_哪個左邊的按鈕?請截圖._ 據我所知,默認行爲是向左滑動,然後確認刪除按鈕。 –

0

您可以通過實施以下委託方法刪除單元格,但是這是一個兩步過程,

  1. 刷卡細胞在右側找到刪除確認按鈕
  2. 單擊刪除(將調用commitEditingStyle

如果你想要做一個單一的步驟/點擊,在UITableViewCell,並在其選擇得到UITableViewCellindexpath從數據源中刪除對象並重新裝載表添加自定義按鈕,這是類似的代碼採用commitEditingStyle方法實施。


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return @"Delete"; 
} 

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Delete the object at `indexPath` from datasource 
    //Update UI, by reloading section/entire table 
}