代码之家  ›  专栏  ›  技术社区  ›  iand675

如何设置UITableViewCell的附件颜色?

  •  27
  • iand675  · 技术社区  · 14 年前

    我想将附件视图图标设置为白色,类似于图标在黑色选项卡中自动变为白色,但我不知道如何执行此操作。有人对此有经验吗?

    9 回复  |  直到 6 年前
        1
  •  10
  •   Matthias Bauch    11 年前

    你最好的办法是制作一个你想放进去的图像,然后将附件视图设置为该图像。这里是 Apple's Official Documentation for UITableViewCells .

        2
  •  77
  •   JackyJohnson    11 年前

    事实上,正确的答案(尽管已经很晚了)是将Cell TintColor属性设置为白色 UIColor 对象,就像这样:

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

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

    注意:更正了属性名,也注意到它实际上不适用于指示器附件类型

        3
  •  6
  •   Akhil K C    9 年前

    为表视图设置淡色可以解决这个问题。

        4
  •  4
  •   Rahul Patel user2885077    10 年前
    UIImageView *checkmark = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"coloredCheckmark.png"]];
    cell.accessoryView = checkmark;`
    

    享受

        5
  •  2
  •   William Hu    8 年前

    银行代码:3.0

    两种方式:

    1. 使用你自己的 UIImageView 作为 accessoryView TableView单元格的。
    2. 如果您使用的是系统附件类型,例如 Disclosure Indicator 箭头图标(>),您可以更改 ui图像视图 tintColor 去实现它。如下所示:

      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
                      }
                  })
              }
          })
      
        6
  •  0
  •   BharathRao    6 年前

    如果在附件视图中使用自定义图像/按钮,则需要更改按钮的淡色,例如

            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;
    
        7
  •  0
  •   Hemang    6 年前
    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
    }
    
        8
  •  0
  •   Kiattisak Anoochitarom    6 年前

    我能做的最简单的方法是“自己从图像创建附件”,然后用模板颜色选项更改颜色,或者只使用具有您想要的颜色的图像。

        9
  •  -1
  •   Ilker Baltaci    8 年前

    在自定义单元格类中

    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
    }