【问题标题】:Change color of UITableViewRowAction title text [duplicate]更改 UITableViewRowAction 标题文本的颜色 [重复]
【发布时间】:2016-05-30 18:03:48
【问题描述】:

我想改变 UITableViewRowAction 标题的颜色。我希望“下载”用红色写。

这是按钮的代码 -

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


        let downloadButton = UITableViewRowAction(style: .Normal, title: "Download") { action, index in

            var url: String

            if self.searchController.active {
                url = String(self.filteredPapers[indexPath.row].url)
            } else {
                url = String(self.papers[indexPath.row].url)
            }

            url = url.stringByReplacingOccurrencesOfString(" ", withString: "%20")
            print(url)
            let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

            self.table.editing = false

            Alamofire.download(.GET, url, destination: destination).response { _, _, _, error in
                if let error = error {
                    print("Failed with error: \(error)")
                } else {
                    print("Downloaded file successfully")
                }
            }

        }

        downloadButton.backgroundColor = UIColor(red:0.79, green:0.79, blue:0.81, alpha:1.0)


        return [downloadButton]

    }

【问题讨论】:

  • UITableViewRowAction 没有(公共)API 可以做到这一点。我建议使用众多滑动框架之一(我目前使用SWTableViewCell,但有很多选项)。
  • 这个答案来自@sschale 提到的问题,应该适合你:stackoverflow.com/a/36145706/5143847

标签: ios swift uitableview uitableviewrowaction


【解决方案1】:

由于 UITableViewRowActionUIButton 的一种类型,我们可以通过更改 UIButton 类的外观来更改文本颜色。在您的editActionsForRowAtIndexPath

UIButton.appearance().setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

输出:

【讨论】:

  • 这不会改变应用程序中每个按钮的颜色吗?
  • 是的,它改变了所有的 UIButton
  • @Nathan,您只需通过 UIButton.appearance(whenContainedInInstancesOf: [YourCustomUITableViewCell.self]).setTitleColor(UIColor.redColor(), forState: UIControlState.Normal) 更改表格视图单元格内的按钮/跨度>
猜你喜欢
  • 2015-01-09
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-03
相关资源
最近更新 更多