OGeek|极客世界-中国程序员成长平台

标题: ios - 如何创建两个自定义表格单元格按钮? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:31
标题: ios - 如何创建两个自定义表格单元格按钮?

我正在准备一张表格,当我在其中滑动单元格时,我需要获得两个圆形按钮。每个按钮应该有一个图像和一个标签。

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()
}



Best Answer-推荐答案


首先,你的代码有一些问题:

  1. 您返回 editButtonItem() 方法的结果,该方法基本上丢弃了您的 hello 操作。我将根据它的名称假设此方法返回一个操作,而不是您想要的两个。
  2. 在您的操作处理程序中,您尝试在 self 上设置背景。 block 从其父范围捕获变量,因此该 block 中的 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]
}

我们创建了两个单独的 Action ,分配它们的背景颜色以使用图案图像,并返回一个包含我们两个 Action 的数组。这是改变 UITableViewRowAction 外观所能做的最多的事情 - 我们可以看到 from the docs ,这个类不继承自 UIView

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

关于ios - 如何创建两个自定义表格单元格按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504430/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4