简体   繁体   English

更改单元格选择上的披露指示颜色

[英]Change disclosure indicator color on cell selection

I've read some posts on this, but still can't get the answer to my question. 我已经阅读了一些关于此的帖子,但仍无法得到我的问题的答案。

I have a disclosure indicator. 我有披露指标。 在此输入图像描述

I add it like this: 我这样添加:

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

Everything works fine and is good, the only thing i want to change is when i press down the cell, disclosure indicator changes color to white. 一切都很好,很好,我唯一想改变的是当我按下单元格时,披露指示器将颜色变为白色。 在此输入图像描述 I don't want that. 我不希望这样。 So my question is how to make the disclosure indicator color fixed, and not changing? 所以我的问题是如何使披露指标颜色固定,而不是改变?

You need to use a custom accessory view for this. 您需要使用自定义附件视图。 Syntax: 句法:

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

Here you are assigning an image view with an image accessory.png 在这里,您将使用image accessory.png指定图像视图

to your cell's accessory view. 到您的手机配件视图。

Swift 3 斯威夫特3

Add a custom accessory view: 添加自定义配件视图:

Example 1: 例1:

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

Example 2: 例2:

Nested in an view container for a small source image: 嵌套在小型源图像的视图容器中:

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

you can try with uiimageview to apply cell accessoryType like below code: 您可以尝试使用uiimageview来应用cell accessoryType,如下面的代码:

cell.accessoryView = myAccessoryUIImageView;

cell accessoryview uiimageview not align correctly try below code: cell accessoryview 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! 

Try something like this. 尝试这样的事情。 Probably you'll be able to achieve what you want. 也许你将能够达到你想要的效果。

-(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;
                    }
                }
            }
        }
    }

I was facing the same problem and found even you're struggling with this...though its quite late answer but i sorted out my issue by writing the following line, 我遇到了同样的问题,发现即使你正在努力解决这个问题......虽然答案很晚,但我通过编写以下内容解决了我的问题,

cell.selectionStyle = UITableViewCellSelectionStyleNone;

it stopped accessory type UITableViewCellAccessoryDisclosureIndicator from being white when selected. 选择时,它会阻止附件类型UITableViewCellAccessoryDisclosureIndicator变为白色。

Hope this helped. 希望这有帮助。

Swift 3 - a simple Table View class that can show a disclosure indicator or not using the showsDisclosure boolean when configuring the cell. Swift 3 - 一个简单的Table View类,在配置单元格时可以使用showsDisclosure布尔值显示或不显示。 right now the disclosure arrow tint color is hard coded, but your color choice could easily be passed in through the configure function as well if you wanted a more reusable cell. 现在,显示箭头色调颜色是硬编码的,但如果你想要一个更可重复使用的单元格,你的颜色选择也很容易通过configure函数传递。

Note that the 'chevron' image is set to be a template image in the asset catalog, which means that it can be tinted a color. 请注意,“雪佛龙”图像设置为资产目录中的模板图像,这意味着它可以着色为颜色。 You can alternatively make a UIImage into a template image in code like so: UIImage(named: "chevron")?.withRenderingMode(.alwaysTemplate) 您也可以使用如下代码将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
        }
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM