Lukas
Lukas

Reputation: 2535

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. enter image description here

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. enter image description here I don't want that. So my question is how to make the disclosure indicator color fixed, and not changing?

Upvotes: 9

Views: 17588

Answers (7)

MPG
MPG

Reputation: 251

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.

Hope this helped.


Swift version:

cell.selectionStyle = .none

Upvotes: 1

Natalia
Natalia

Reputation: 1359

Swift 3 - a simple Table View class that can show a disclosure indicator or not using the showsDisclosure boolean when configuring the cell. 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.

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)

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

Upvotes: 0

Peter Kreinz
Peter Kreinz

Reputation: 8636

Swift 3

Add a custom accessory view:

Example 1:

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

Example 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

Upvotes: 3

Harsh
Harsh

Reputation: 2908

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

Upvotes: 2

saksha nagpal
saksha nagpal

Reputation: 7

[[UITableViewCell appearance] setTintColor:[UIColor redColor]];

Upvotes: -3

Dinesh
Dinesh

Reputation: 6532

you can try with uiimageview to apply cell accessoryType like below code:

cell.accessoryView = myAccessoryUIImageView;

cell accessoryview uiimageview not align correctly try below code:

    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! 

Upvotes: 1

Midhun MP
Midhun MP

Reputation: 107141

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

to your cell's accessory view.

Upvotes: 13

Related Questions