【问题标题】:How can I set a UITableViewCell's accessory color?如何设置 UITableViewCell 的附件颜色?
【发布时间】:2010-11-26 03:39:55
【问题描述】:

我想将附件视图图标设置为白色,类似于在黑色 TabBar 中自动将图标设置为白色的方式,但对我来说如何做到这一点并不是很明显。有人有这方面的经验吗?

【问题讨论】:

    标签: iphone ios


    【解决方案1】:
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "MyCustomCellIdentifier", for: indexPath) as? MyCustomCell else { return UITableViewCell() }
        cell.tintColor = UIColor.white //Important
        cell.accessoryType = (someCondition) ? .checkmark : .none
        return cell
    }
    

    【讨论】:

      【解决方案2】:

      在您的自定义单元类中

      override func awakeFromNib() {
         super.awakeFromNib()
         tintColor = UIColor.red
      }
      

      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cellModel = addressCellModels[indexPath.row]
          let cell = tableView.dequeueReusableCell(withIdentifier:cellModel.cellIdentifier, for: indexPath) as! AddressSelectionCell
          cell.tintColor = UIColor.red
          return cell
      }
      

      【讨论】:

      • 会不会是您在其他地方覆盖了 tintcolor?
      • 我没有时间,所以我只使用了自定义单元格
      【解决方案3】:

      实际上正确的答案(尽管为时已晚)是将您的单元格 tintColor 属性设置为白色 UIColor 对象,如下所示:

      UITableViewCell *cell = [UITableViewCell new]; // or other instantiation...
      [cell setTintColor:[UIColor whiteColor]];
      

      完全子类化附件视图也是可能的,但问题是要求使单元格附件按钮变白,所以这应该是正确的答案(至少对于iOS7)。

      注意:更正了属性名称,还请注意它实际上不适用于指标配件类型

      【讨论】:

      • UITableViewCell 上的属性实际上是 tintColor - 而不是 barTint。
      • 关于指标附件类型不工作的说明是让我绊倒的原因,很好!
      • 没有人使用 ios6 并且这在 ios8 中仍然有效
      • 此时如果它停止工作,他们似乎会破坏很多人的代码:)
      【解决方案4】:

      结合@BharathRao、@Hemang 和@Kiattisak Anochitarom 想法的解决方案

      这似乎是 iOS 13+ 中的一个问题(更改 tintColor 不会有任何区别),Apple Developer Forums 上此页面的浏览量超过 2000 (https://forums.developer.apple.com/thread/121472)

      我使用@BharathRao 和@Hemang 的想法的解决方案是向UITableViewCell 添加一个扩展名为eg:addDisclosureIndicator

      如果有多个表格和情节提要,使用扩展程序将使应用程序中的内容更加统一。

      extension UITableViewCell {
             func addDisclosureIndicator(){
                let button = UIButton(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
          /// 15 look great for me in iPhone and iPad
                 button.setImage( UIImage(named: "rightArrow"), for: .normal) 
              ///You can download a chevron of your choice online
                 button.tintColor = UIColor(yourPreferredColor)
                 self.accessoryView = button
      
         }
       }
      

      打电话

      override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
          {
              let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
              cell.addDisclosureIndicator()
      
              return cell
          }
      

      希望这有帮助。

      【讨论】:

        【解决方案5】:

        您最好的办法是制作一张您想要放入其中的图像,然后将附件视图设置为该图像。这里是Apple's Official Documentation for UITableViewCells

        【讨论】:

        • 这是没有必要的。 @believesInSanta 的回答是正确的。
        • 这其实是iOS7之前的正确做法。
        【解决方案6】:

        Swift 3.0

        两种方式:

        1. 使用您自己的UIImageView 作为表格视图单元格的accessoryView
        2. 如果您使用的是系统附件类型,如Disclosure Indicator箭头图标(>),您可以更改UIImageViewtintColor来实现。如下:

          cell?.subviews.forEach({
                  if let btn = $0 as? UIButton {
                      btn.subviews.forEach({
                          if let imageView = $0 as? UIImageView {
                              let image = imageView.image?.withRenderingMode(.alwaysTemplate)
                              imageView.image = image
                              imageView.tintColor = UIColor.red
                          }
                      })
                  }
              })
          

        【讨论】:

          【解决方案7】:
          UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coloredCheckmark.png"]];
          cell.accessoryView = checkmark;`
          

          享受

          【讨论】:

          • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post
          • 在附件视图中添加自定义图像的最佳方式
          • 我也反对给整个细胞染色。这个很完美。谢谢
          【解决方案8】:

          为表格视图设置色调颜色为我解决了这个问题。

          【讨论】:

            【解决方案9】:

            如果您为附件视图使用自定义图像/按钮,则需要更改按钮的色调颜色,例如

                    UIImageView *trash_icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_delete_record.png"]];
                    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    button.frame = CGRectMake(280, 140, 30, 30);
            
                    //Here is where you change the color of the button.
                    button.tintColor = UIColor.whiteColor;
            
                    [button setImage: trash_icon.image forState:UIControlStateNormal];
                    cell.accessoryView  = button;
            

            【讨论】:

              【解决方案10】:

              我会做的最简单的方法是“自己从图像创建配件”并使用模板颜色选项更改颜色,或者只使用具有所需颜色的图像。

              【讨论】:

                猜你喜欢
                • 2015-03-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-08-21
                • 2020-07-01
                • 1970-01-01
                相关资源
                最近更新 更多