22

disclosureIndicatorViewのアクセサリの色を変更する必要がありUITableViewCellます。これを行うには2つの方法があると思いますが、どちらが最適かわかりません。そこで、私ができると思うことはここにあります。

UITableViewCell~の性質がありますaccessoryView。そのため、必要な画像を保持するためにビューを使用setAccessoryView:(UIView *)viewして渡すことができます。UIImageView

セルのコンテンツ ビュー (背景色、その他のものの追加など) を作成するユーティリティ クラスを作成し、このコンテンツ ビューをセルに追加しUITableViewDelegateます。もう 1 つのオプションは、ユーティリティ クラスのメソッドをUIImageオーバーライドすることです。drawRectCustomContentView

オプション 1 を実行する - 私はアップルのやり方で物事を成し遂げることができます。ビューを提供するだけで、残りは彼らが行います。UIViewしかし、すべての行に新しいオブジェクトを追加すると、オブジェクトの割り当てが重くなり、フレームレートが低下する可能性があると思います。myの単なるUIImageオブジェクトと比較してcontentViewUIImageより軽いと思いますUIView

軽い人を投げて、私がそれを決めるのを手伝ってください。

4

12 に答える 12

29

これに対処するCocoaneticsの素晴らしい投稿。UIControl クラスは、選択、有効化、および強調表示されたプロパティを継承します

于 2011-04-05T17:33:07.313 に答える
12

iOS 8 以降で動作する実装を次に示します。
元の Apple 開示インジケータの色をカスタム色に変更します
次のように使用します。

#import "UITableViewCell+DisclosureIndicatorColor.h"
// cell is a UITableViewCell
cell.disclosureIndicatorColor = [UIColor redColor]; // custom color
[cell updateDisclosureIndicatorColorToTintColor]; // or use global tint color

UITableViewCell+DisclosureIndicatorColor.h

@interface UITableViewCell (DisclosureIndicatorColor)
@property (nonatomic, strong) UIColor *disclosureIndicatorColor;
- (void)updateDisclosureIndicatorColorToTintColor;
@end

UITableViewCell+DisclosureIndicatorColor.m

@implementation UITableViewCell (DisclosureIndicatorColor)

- (void)updateDisclosureIndicatorColorToTintColor {
    [self setDisclosureIndicatorColor:self.window.tintColor];
}

- (void)setDisclosureIndicatorColor:(UIColor *)color {
    NSAssert(self.accessoryType == UITableViewCellAccessoryDisclosureIndicator,
        @"accessory type needs to be UITableViewCellAccessoryDisclosureIndicator");

    UIButton *arrowButton = [self arrowButton];
    UIImage *image = [arrowButton backgroundImageForState:UIControlStateNormal];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    arrowButton.tintColor = color;
    [arrowButton setBackgroundImage:image forState:UIControlStateNormal];
}

- (UIColor *)disclosureIndicatorColor {
    NSAssert(self.accessoryType == UITableViewCellAccessoryDisclosureIndicator, 
        @"accessory type needs to be UITableViewCellAccessoryDisclosureIndicator");

    UIButton *arrowButton = [self arrowButton];
    return arrowButton.tintColor;
}

- (UIButton *)arrowButton {
    for (UIView *view in self.subviews)
        if ([view isKindOfClass:[UIButton class]])
            return (UIButton *)view;
    return nil;
}

@end
于 2016-02-16T08:40:04.900 に答える
5

UIImageView を使用します。これにより、セルが選択されているときに画像を変更することもできます。

UIImageView* arrowView = [[UIImageView alloc] initWithImage:normalImage];
arrowView.highlightedImage = selectedImage;
cell.accessoryView = arrowView;
[arrowView release];
于 2012-07-06T15:09:40.667 に答える
4

しかし、すべての行に新しい UIView オブジェクトを追加すると、obj の割り当てが多くなり、フレーム レートが低下する可能性があると思います。contentView の単なる UIImage オブジェクトと比較して。UIImage は UIView より軽いと思います。

画像を直接描画すると、サブビューを追加するよりも確実にパフォーマンスが向上します。その追加のパフォーマンスが必要かどうかを判断する必要があります。基本セルのカスタム開示インジケーターにいくつかのアクセサリ ビューを使用しましたが、パフォーマンスは問題ありませんでした。ただし、コンテンツ rect のカスタム描画を既に行っている場合は、アクセサリ ビューもそれほど難しくないかもしれません。

于 2009-12-06T06:16:56.910 に答える
2

benzado のソリューションは正常に機能しますが、黒い背景が表示されました。セットアップする UIView クラス (コードに追加した drawRect 関数のクラス) では、開示描画の背景を透明にするために、次の initWithFrame 実装が必要です。

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        // Initialization code.
    }
    return self;
}

もちろん、これを好きな色に設定できます...

于 2011-01-12T18:33:34.133 に答える
1

galambalazs の回答は機能しますが、Apple の非公開の開示インジケーターの実装に間接的にアクセスして更新するため、ややハックであることに注意してください。せいぜい、将来の iOS リリースで機能しなくなる可能性があり、最悪の場合、App Store で拒否される可能性があります。を設定することaccessoryViewは、Apple が色を直接設定するためのプロパティを公開するまで承認された方法です。

それにもかかわらず、これは、それを望むかもしれない人のための彼の答えのSwift実装です:

注:cell.disclosureIndicatorColorが設定された後 cell.accessoryType = .DisclosureIndicatorに設定する必要があるため、セルのサブビューで discoveryIndi​​cator ボタンを使用できます。

extension UITableViewCell {

    public var disclosureIndicatorColor: UIColor? {
        get {
            return arrowButton?.tintColor
        }
        set {
            var image = arrowButton?.backgroundImageForState(.Normal)
            image = image?.imageWithRenderingMode(.AlwaysTemplate)
            arrowButton?.tintColor = newValue
            arrowButton?.setBackgroundImage(image, forState: .Normal)
        }
    }

    public func updateDisclosureIndicatorColorToTintColor() {
        self.disclosureIndicatorColor = self.window?.tintColor
    }

    private var arrowButton: UIButton? {
        var buttonView: UIButton?
        self.subviews.forEach { (view) in
            if view is UIButton {
                buttonView = view as? UIButton
                return
            }
        }
        return buttonView
    }
}
于 2016-04-06T14:52:54.633 に答える
0

@benzado のソリューションへの貢献として、次のように彼のコードを迅速化しました。

override func drawRect(rect: CGRect) {

  super.drawRect(rect)

  let context = UIGraphicsGetCurrentContext();

  let right_margin : CGFloat = 15.0
  let length : CGFloat = 4.5;

  // (x,y) is the tip of the arrow
  let x = CGRectGetMaxX(self.bounds) - right_margin;
  let y = CGRectGetMidY(self.bounds);

  CGContextMoveToPoint(context, x - length, y - length);
  CGContextAddLineToPoint(context, x, y);
  CGContextAddLineToPoint(context, x - length, y + length);
  CGContextSetLineCap(context, .Round);
  CGContextSetLineJoin(context, .Miter);
  CGContextSetLineWidth(context, 2.5);

  if (self.highlighted)
  {
    CGContextSetStrokeColorWithColor(context, UIColor.appColorSelected().CGColor);
  }
  else
  {
    CGContextSetStrokeColorWithColor(context, UIColor.appColor().CGColor);
  }

  CGContextStrokePath(context);
}

アプリの色を変更すると、UITableCellView で setNeedsDisplay() を呼び出すと色が更新されます。セル ビューでの UIImage オブジェクトの使用は避けたいと思います。

于 2016-06-15T10:28:42.707 に答える