web-dev-qa-db-fra.com

Comment changer la couleur de l'indicateur de divulgation de UITableView par programme?

Je sais qu’en utilisant UIImageView Nous pouvons définir l’accessoire indicateur de divulgation, mais je souhaite modifier uniquement la couleur de l’indicateur de divulgation sans utiliser UIImageView.

Est-ce possible ou non? Si c'est possible alors, comment? 

24
Bhavesh

Ajoutez votre propre indicateur de divulgation:

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

Je sais que je suis en retard pour la fête, mais je viens de préparer une vue personnalisée qui trace une vue de divulgation. Je l'ai rapidement faite pour une démo, donc ce n'est peut-être pas très précis, ça fait juste le travail. J'espère que ça aide quelqu'un:

PZDisclosureIndicator.h

//
// Created by Pall Zoltan on 10/10/14.
// Copyright (c) 2014 pallzoltan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface PZDisclosureIndicator : UIView
@property(nonatomic, strong) UIColor *color;

- (PZDisclosureIndicator *)initWithColor:(UIColor *)color;
@end

PZDisclosureIndicator.m:

//
// Created by Pall Zoltan on 10/10/14.
// Copyright (c) 2014 pallzoltan. All rights reserved.
//

#import "PZDisclosureIndicator.h"

@interface PZDisclosureIndicator ()

@property(nonatomic) CGFloat red;
@property(nonatomic) CGFloat green;
@property(nonatomic) CGFloat blue;
@property(nonatomic) CGFloat alpha;

@end

@implementation PZDisclosureIndicator

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, self.red, self.green, self.blue, self.alpha);

    CGContextMoveToPoint(context, 4, 0);
    CGContextAddLineToPoint(context, 4, 0);
    CGContextAddLineToPoint(context, 16, 12);
    CGContextAddLineToPoint(context, 4, 24);
    CGContextAddLineToPoint(context, 0, 24 - 4);
    CGContextAddLineToPoint(context, 9, 12);
    CGContextAddLineToPoint(context, 0, 4);
    CGContextAddLineToPoint(context, 4, 0);
    CGContextFillPath(context);
}

- (PZDisclosureIndicator *)initWithColor:(UIColor *)color {
    self = [super initWithFrame:CGRectMake(0, 0, 16, 24)];
    self.backgroundColor = [UIColor clearColor];

    self.color = color;
    [self setNeedsDisplay];

    return self;
}

- (void)setColor:(UIColor *)color {
    _color = color;

    [self.color getRed:&_red green:&_green blue:&_blue alpha:&_alpha];

    [self setNeedsDisplay];
}

@end

Ensuite, dans ma cellule personnalisée, je fais ceci:

self.accessoryView = [[PZDisclosureIndicator alloc] initWithColor:SECONDARY_HIGHLIGHT_COLOR];

Théoriquement, vous devriez être capable de changer sa couleur après l'initialisation aussi, sans l'avoir essayé.

Ressemble à ça:Sample usage of the view

Z.

4
Zoltán

Désolé, je suis super super tard pour la fête mais je me débattais avec cela aujourd'hui et je l'avais compris (sous iOS8 et 9). À l'aide du débogueur de vues, il était clair que le triangle de divulgation était un UIImage dans un UIImageView dans un UIButton.

Donc, dans -awakeFromNib, je parcourais les sous-vues pour trouver un bouton. Une fois le bouton trouvé, une référence à l'image d'origine pourrait être acquise via -backgroundImageForState:

Une fois que vous obtenez l'image d'origine, vous pouvez créer une copie qui est une image modèle en appelant [originalImage imageWithRenderingMode: AlwaysTemplate].

Ensuite, vous pouvez définir backgroundImage pour tous les états disponibles. Une fois cette opération effectuée, l’image reprend automatiquement la couleur de teinte. 

Voici un exemple de code Swift:

class GratuitousDisclosureTableViewCell: UITableViewCell {

    private weak var disclosureButton: UIButton? {
        didSet {
            if let originalImage = self.disclosureButton?.backgroundImageForState(.Normal) {
                let templateImage = originalImage.imageWithRenderingMode(.AlwaysTemplate)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Normal)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Highlighted)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Disabled)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Selected)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Application)
                self.disclosureButton?.setBackgroundImage(templateImage, forState: .Reserved)
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        for view in self.subviews {
            if let button = view as? UIButton {
                self.disclosureButton = button
                break
            }
        }  
    }
}
3
Jeffrey Bergier

Comme indiqué ci-dessus, une façon de modifier un accessoireView consiste à ajouter votre propre UIImageView. Cependant, en réalité, vous pouvez fournir n’importe quel objet dérivé d’UIView. Je recommanderais alors d’utiliser un UILabel avec une police d’icônes (par exemple, icomoon) au lieu de UIImageView. UILabel et une police d'icônes permettent une grande flexibilité en termes d'image, de couleur et de taille. 

let font = UIFont(name: "icomoon", size: 16.0)
let icon = "\u{e60f}"    
let lbl = UILabel(frame: CGRectMake(0, 0, 20, 20))
lbl.font = font
lbl.text = icon
cell.accessoryView = lbl
1
Gooshan

Vous devez créer votre propre UIButton personnalisé et le définir en tant que cellule accessoireVoir. Vous ne pouvez pas changer sa couleur en spécifiant simplement son UIColor.

Vous pouvez personnaliser l'indicateur de divulgation en ajoutant une image pour l'état normal et une autre pour l'image sélectionnée (en surbrillance).

une bibliothèque pour personnaliser l'indicateur de divulgation: cliquez ici .

1
Mumthezir VP

Vous pouvez essayer avec uiimageview d'appliquer le type d'accessoire de cellule comme ci-dessous:

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];
0
TechFanatic