2016-09-15 260 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继承。

如果您想更多地定制外观,您应该寻找外部库或从头开始实施您自己的解决方案。

相关问题