【问题标题】:How do I set the background color of the editing accessory of a table view?如何设置表格视图的编辑附件的背景颜色?
【发布时间】:2016-05-09 08:15:04
【问题描述】:

我的应用程序的主题颜色是绿色,所以一切都必须是绿色的,就像这个表格视图单元格:

问题是当我单击编辑按钮时,单元格左侧会弹出一个小减号,并且不是绿色:

我不明白为什么会这样。这是我在cellForRowAtIndexPath中的代码

let cell = UITableViewCell()
cell.textLabel?.text = someStuff
cell.textLabel?.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
cell.contentView.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)

// Look! here I set the color of the editing accessory!
cell.editingAccessoryView?.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
return cell

如您所见,我已将所有内容设置为绿色,包括文本标签、背景,甚至editingAccessoryView!但那东西不是绿色的!如上所示,它保持白色。

我还需要设置什么东西才能让它变成绿色吗?

【问题讨论】:

    标签: ios swift uitableview colors


    【解决方案1】:

    您必须在 tableView:willDisplayCell:forRowAtIndexPath: 方法中执行此操作。你可以这样:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "MyText"
        return cell
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
    }
    

    编辑:

    这是上面代码的视觉结果:

    【讨论】:

      【解决方案2】:

      该函数的方法签名在 swift 的后续版本中略有变化。

      在 Swift 4+ 中使用

      override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
              cell.backgroundColor = UIColor.red // Your color here!
      }
      

      【讨论】:

        【解决方案3】:

        editingAccessoryView的背景颜色可以这样修改:

        cell.contentView.superview.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
        

        如果contentView.superview背景颜色的改变不起作用你可以试试这个:

        func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
             cell.backgroundColor = UIColor(red: 0xca / 0xff, green: 1, blue: 0xc7 / 0xff, alpha: 1)
        }
        

        【讨论】:

          【解决方案4】:

          这就是我为 Swift 2.2 更新按钮的方式

          override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
              {
                  let deleteBtn = UITableViewRowAction(style: .Default, title: "Delete", handler: { (action, indexPath) in
                      print("Delete pressed!")
                  })
                  deleteBtn.backgroundColor = UIColor.blueColor()//Change color here
                  let editBtn = UITableViewRowAction(style: .Default, title: "Edit", handler: { (action, indexPath) in
                      print("Edit pressed!")
                  })
                  editBtn.backgroundColor = UIColor.redColor()//change color here
                  return [deleteBtn, editBtn]
              }
          
              override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
                  return true
              }
              override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
              }
          

          【讨论】:

            【解决方案5】:

            不知道为什么苹果不把这些东西默认。这是更改整个 UITableViewCell 背景颜色的代码:

            public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cellView = tableView.dequeueReusableCell(withIdentifier: cellReuseId, for: indexPath)
                //Setup selectedbackgroundView which change color includes editingAccessoy background  color
                let selectedBackground = UIView()
                selectedBackground.frame = cellView.frame
                selectedBackground.backgroundColor = SCColor.selectedColor
                cellView.selectedBackgroundView = selectedBackground
                //Setup backgroundView which change color includes asscessory background color
                let cellBackground = UIView()
                cellBackground.frame = cellView.frame
                cellBackground.backgroundColor = cellData.isSelected ? SCColor.selectedColor : UIColor.white
                cellView.backgroundView = cellBackground
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-01-11
              • 1970-01-01
              • 2010-10-28
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-03-11
              • 1970-01-01
              相关资源
              最近更新 更多