如何将图像放在UITableViewRowAction标题之上?

时间:2016-03-11 18:11:28

标签: swift swift2

我已经搜索过这个问题,但我发现的是:

  1. 如何将图像作为行动作背景。

  2. 如何设置unicode字符\ u {unicode here}。它有效,但我 找不到用于表示添加按钮的unicode字符,或者 删除按钮也许。

  3. 但我想要的是将自定义图像放在行动作标题的顶部。例如:垃圾桶下方标题为delete。

    我也试过这段代码:

    "\(UIImage(named: "trashImg"))"
    

    在行动作标题字符串中,但xcode似乎不计算最后一个)作为代码的一部分,但仅作为字符串的一部分。但无论如何,我构建了这个项目,当我测试时,而不是标题,有一个巨大的文字说“可选:(64,)......等等,它没有崩溃。

    请帮助我,许多应用程序都这样做,我也想要它!

    提前致谢。

2 个答案:

答案 0 :(得分:2)

您需要根据您的要求更改插入内容。

class TableViewRowAction: UITableViewRowAction 
{
    var image: UIImage?

    func _setButton(button: UIButton)  {

        if let image = image, let titleLabel = button.titleLabel {

             let labelString = NSString(string: titleLabel.text!)
             let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel.font])
             button.tintColor = UIColor.whiteColor()
             button.setImage(image.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal)
             button.imageEdgeInsets.right = -titleSize.width
        }
    }
}


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {


    let moreRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "        ") { action, indexPath in
        // Action Block of more button.
    }

    moreRowAction.image = UIImage(named: "edit_white")
    moreRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)



    let deleteRowAction = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "        ") { action, indexPath in
          // Action Block of delete button.

    }

    deleteRowAction.backgroundColor = UIColor(red: 252/255, green: 65/255, blue: 75/255, alpha: 1.0)
    deleteRowAction.image = UIImage(named: "delete_white")

    return [deleteRowAction, moreRowAction]

}

答案 1 :(得分:0)

您将图像放入一个字符串对象,该对象将获得该图像的默认字符串表示形式。

也许这篇文章可能有所帮助。看起来它们会覆盖editActionsForRowAtIndexPath并使用patternimage设置背景颜色。 Custom table view row action (image)

另一篇帖子说的基本相同UITableViewRowAction image for title

相关问题