2016-09-15 232 views
2

我正在準備一個表格,當我滑動單元格時,我需要獲得兩個圓角按鈕。每個按鈕應該有一個圖像和一個標籤。所有的如何創建兩個自定義表格單元格按鈕?

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 
    var hello = UITableViewRowAction(style: .Default, title: "Image") { (action, indexPath) in 

    // do some action 

    if let buttonImage = UIImage(named: "Image") { 
     // self.bgColor = UIColor.imageWithBackgroundColor(image: buttonImage, bgColor: UIColor.blueColor()) 
    } 
    return editButtonItem() 
} 

回答

0

首先,有一些問題,你的代碼:

  1. 將返回editButtonItem()方法,基本上是放棄你的hello作用的結果。我會從它的名字中假設,這種方法返回了一個單一的動作,而不是你想要的。
  2. 在您的動作處理程序中,您試圖在self上設置背景。從它們的父作用域中捕獲變量,因此該塊中的selfhello操作無關,而是與執行editActionsForRowAtIndexPath方法的類相關。

如何實現你所需要的(與標題和圖片兩個按鈕):

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 
    var firstAction = UITableViewRowAction(style: .Default, title: "First") { (action, indexPath) in 
     // action handler code here 
     // this code will be run only and if the user presses the button 
     // The input parameters to this are the action itself, and indexPath so that you know in which row the action was clicked 
    } 
    var secondAction = UITableViewRowAction(style: .Default, title: "Second") { (action, indexPath) in 
     // action handler code here 
    } 

    firstAction.backgroundColor = UIColor(patternImage: UIImage(named: "firstImageName")!) 
    secondAction.backgroundColor = UIColor(patternImage: UIImage(named:"secondImageName")!) 

    return [firstAction, secondAction] 
} 

我們創建兩個單獨的行動,指派他們的背景顏色使用模式的圖像,並返回一個包含了我們的行動數組。這是你可以做的最多的改變UITableViewRowAction的外觀 - 我們可以看到from the docs,這個類不會從UIView繼承。

如果您想更多地定製外觀,您應該尋找外部庫或從頭開始實施您自己的解決方案。

相關問題