【问题标题】:Change disclosure indicator color on cell selection更改单元格选择的披露指示器颜色
【发布时间】:2012-07-09 14:25:18
【问题描述】:

我已经阅读了一些关于此的帖子,但仍然无法得到我的问题的答案。

我有一个披露指标。

我是这样添加的:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

一切正常,很好,我唯一想要改变的是当我按下单元格时,披露指示器将颜色变为白色。 我不想要那个。所以我的问题是如何使披露指示器颜色固定而不改变?

【问题讨论】:

    标签: iphone objective-c ios ipad


    【解决方案1】:

    您需要为此使用自定义附件视图。 语法:

    cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory.png"]] autorelease];
    

    在这里,您正在为 image view 分配图片 accessory.png

    到您的单元格的附件视图。

    【讨论】:

    • 但是没有别的办法了吗?我不想制作自己的自定义视图,因为我的应用程序中有多个表格,所以这将是很多工作,我正在寻找一些更简单的方法:)
    • 不,这是 UITableViewCells 的默认行为,如果你想覆盖它,你需要添加自己的自定义附件视图。
    • 就这样完成了,希望有更好的方法,但是很好,它有效..感谢您的帮助!
    【解决方案2】:

    斯威夫特 3

    添加自定义附件视图:

    示例 1:

    cell.accessoryView = UIImageView(image: UIImage(named: "accessory.png"))
    

    示例 2:

    嵌套在一个小源图像的视图容器中:

    let accessoryView = UIView(frame: CGRect(x: 0, y: 0, width: 24, height: 50))
    let accessoryViewImage = UIImageView(image: UIImage(named: "accessory.png"))
    accessoryViewImage.center = CGPoint(x: 12, y: 25)
    accessoryView.addSubview(accessoryViewImage)
    
    cell.accessoryView = accessoryView
    

    【讨论】:

      【解决方案3】:

      试试这样的。也许你将能够实现你想要的。

      -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
              for (id obj in cell.subviews){
                  if([obj isKindOfClass:[UIButton class]]) {
                      UIButton *button = obj;
                      for(id btnObj in button.subviews){
                          if([btnObj isKindOfClass:[UIImageView class]]) {
                              UIImageView *imgView = btnObj;
                              UIImage *image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
                              imgView.image = image;
                              imgView.tintColor = cell.backgroundColor;
                          }
                      }
                  }
              }
          }
      

      【讨论】:

        【解决方案4】:

        您可以尝试使用 uiimageview 来应用单元格附件类型,如下所示:

        cell.accessoryView = myAccessoryUIImageView;
        

        cell Accessoriesview uiimageview 没有正确对齐试试下面的代码:

            UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
            UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"accessory_image.png"]];
            accessoryViewImage.center = CGPointMake(12, 25);
            [accessoryView addSubview:accessoryViewImage];
            [cell setAccessoryView:accessoryView];
            [accessoryViewImage release];//not use this line your ios sdk in 5.0 and above! 
            [accessoryView release];//not use this line your ios sdk in 5.0 and above! 
        

        【讨论】:

          【解决方案5】:

          我遇到了同样的问题,发现即使你也在为此苦苦挣扎......虽然它的答案很晚,但我通过写下一行来解决我的问题,

          cell.selectionStyle = UITableViewCellSelectionStyleNone;

          它阻止了配件类型 UITableViewCellAccessoryDisclosureIndicator 在被选中时变为白色。

          希望这会有所帮助。


          Swift 版本:

          cell.selectionStyle = .none

          【讨论】:

          • 这应该是一个正确/最短的答案。
          【解决方案6】:

          Swift 3 - 一个简单的表格视图类,可以在配置单元格时显示或不使用 showsDisclosure 布尔值显示披露指示符。现在公开箭头的色调颜色是硬编码的,但是如果您想要一个更可重用的单元格,您也可以通过配置函数轻松地传递您的颜色选择。

          请注意,“chevron”图像设置为资产目录中的模板图像,这意味着它可以着色。您也可以使用如下代码将 UIImage 制作成模板图像:UIImage(named: "chevron")?.withRenderingMode(.alwaysTemplate)

          class HeaderTableViewCell: UITableViewCell {
          
              @IBOutlet weak var sectionTitle: UILabel!
          
              override func awakeFromNib() {
                  sectionTitle.textColor = .black
              }
          
              func configure(title: String, showsDisclosure: Bool = false) {
                  self.sectionTitle.text = title
          
                  if showDisclosure {
                      //add custom disclosure arrow
                      let chevronImgView = UIImageView(image: UIImage(named: "chevron"))
                      chevronImgView.tintColor = .red
                      self.accessoryType = .disclosureIndicator
                      self.accessoryView = chevronImgView
                  } else {
                      self.accessoryType = .none
                      self.accessoryView = nil
                  }
              }
          }
          

          【讨论】:

            【解决方案7】:

            [[UITableViewCell 外观] setTintColor:[UIColor redColor]];

            【讨论】:

            • 披露指示器上的图像未设置为 AlwaysTemplate,因此这不起作用。
            猜你喜欢
            • 2018-10-20
            • 2010-11-19
            • 2020-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多