哪个是在iOS中的表格视图单元格中更改公开指示器附件视图的颜色/视图的最佳方法?

AJ.*_*AJ. 21 indicator accessory uitableview ios

我需要改变disclosureIndicatorView配件的颜色UITableViewCell.我认为有两种方法可以完成这项任务,但我无法弄清楚哪一种是最佳选择.所以这就是我认为我能做到的.

有一个属性UITableViewCell- accessoryView.所以我可以使用setAccessoryView:(UIView *)view并传递视图作为UIImageView持有我想要的图像.

我编写了一个实用程序类,它为我的单元格创建内容视图(像背景颜色,添加其他东西等等),然后将此内容视图添加到单元格中UITableViewDelegate.另一种选择是绘制UIImage覆盖实用程序类的drawRect方法CustomContentView.

执行选项1 - 我可以通过苹果方式完成工作.只要给他们观点,他们就会完成其余的工作.但我想UIView在每一行中添加一个新对象可能会成为一个繁重的对象分配并降低帧速率.与UIImage我的一个对象相比contentView.我相信UIImage比较轻UIView.

请抛出一些轻松的人并帮我决定.

Nat*_*ter 29

关于Cocoanetics的伟大帖子.UIControl类继承选定,启用和突出显示的自定义颜色披露指标属性


ben*_*ado 24

如果您对绘制指标感兴趣,而不是使用图像文件,那么我的代码就是这样做的:

// (x,y) is the tip of the arrow
CGFloat x = CGRectGetMaxX(self.bounds) - RIGHT_MARGIN;
CGFloat y = CGRectGetMidY(self.bounds);
const CGFloat R = 4.5;
CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctxt, x-R, y-R);
CGContextAddLineToPoint(ctxt, x, y);
CGContextAddLineToPoint(ctxt, x-R, y+R);
CGContextSetLineCap(ctxt, kCGLineCapSquare);
CGContextSetLineJoin(ctxt, kCGLineJoinMiter);
CGContextSetLineWidth(ctxt, 3);
// If the cell is highlighted (blue background) draw in white; otherwise gray
if (CONTROL_IS_HIGHLIGHTED) {
    CGContextSetRGBStrokeColor(ctxt, 1, 1, 1, 1);
} else {
    CGContextSetRGBStrokeColor(ctxt, 0.5, 0.5, 0.5, 1);
}
CGContextStrokePath(ctxt);
Run Code Online (Sandbox Code Playgroud)

如果您创建一个自定义的UIView子类,请在drawRect:方法中执行上述操作,并将其用作附件视图,您将能够将颜色设置为您想要的颜色.

附件视图(自定义或UIImageView不会成为主要的性能问题,只要您正确地回收UITableViewCell实例.


gbl*_*zex 11

这是一个适用于iOS 8+的实现.它完全符合要求:
将原始Apple披露指示器的颜色更改为自定义颜色.
像这样使用它:

#import "UITableViewCell+DisclosureIndicatorColor.h"
// cell is a UITableViewCell
cell.disclosureIndicatorColor = [UIColor redColor]; // custom color
[cell updateDisclosureIndicatorColorToTintColor]; // or use global tint color
Run Code Online (Sandbox Code Playgroud)

的UITableViewCell + DisclosureIndicatorColor.h

@interface UITableViewCell (DisclosureIndicatorColor)
@property (nonatomic, strong) UIColor *disclosureIndicatorColor;
- (void)updateDisclosureIndicatorColorToTintColor;
@end
Run Code Online (Sandbox Code Playgroud)

的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
Run Code Online (Sandbox Code Playgroud)


Ste*_*Orr 7

在第三版中,我将@galambalazs的解决方案作为类扩展进行了调整,如下所示:

import UIKit

extension UITableViewCell {

    func setDisclosure(toColour: UIColor) -> () {
        for view in self.subviews {
            if let disclosure = view as? UIButton {
                if let image = disclosure.backgroundImage(for: .normal) {
                    let colouredImage = image.withRenderingMode(.alwaysTemplate);
                    disclosure.setImage(colouredImage, for: .normal)
                    disclosure.tintColor = toColour
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助。


cha*_*e55 5

使用UIImageView.这也允许您在选择单元格时更改图像:

UIImageView* arrowView = [[UIImageView alloc] initWithImage:normalImage];
arrowView.highlightedImage = selectedImage;
cell.accessoryView = arrowView;
[arrowView release];
Run Code Online (Sandbox Code Playgroud)


amr*_*rox 4

但我猜想向每一行添加一个新的 UIView 对象可能会导致大量的 obj 分配并降低帧速率。与我的 contentView 中的 UIImage 对象相比。我相信 UIImage 比 UIView 更轻。

直接绘制图像几乎肯定比添加子视图具有更好的性能。您必须确定是否需要额外的性能。我在基本单元格上使用了一些辅助视图来自定义披露指示器,并且性能很好。但是,如果您已经在对内容矩形进行自定义绘制,那么执行附件视图可能也不那么困难。