iphone - 更改自定义 UITableViewCell iphone 中的文本颜色

标签 iphone ipad uitableview

我有一个自定义单元格,当用户选择该单元格时,我希望两个 UILabels 中的文本更改为浅灰色。

ChecklistCell.h:

#import <UIKit/UIKit.h>


@interface ChecklistCell : UITableViewCell {
    UILabel *nameLabel;
    UILabel *colorLabel;
    BOOL selected;


}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@property (nonatomic, retain) IBOutlet UILabel *colorLabel;



@end

ChecklistCell.m:

#import "ChecklistCell.h"


@implementation ChecklistCell
@synthesize colorLabel,nameLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
        // Initialization code
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)dealloc {
    [nameLabel release];
    [colorLabel release];
        [super dealloc];
}


@end

最佳答案

由于您使用的是自定义表格单元格,因此您可以通过在自定义 UITableViewCell 中实现 setSelected 和 setHighlighted 方法来实现代码来设置标签颜色。这将捕获选择中的所有状态更改,尽管存在一些棘手的情况,例如当您在选择单元格后选择并拖动单元格外部时,以 NO 调用 setHighlighting 时。这是我使用的方法,我相信该方法在所有情况下都可以适本地设置颜色。

- (void)updateCellDisplay {
    if (self.selected || self.highlighted) {
        self.nameLabel.textColor = [UIColor lightGrayColor];
        self.colorLabel.textColor = [UIColor lightGrayColor];
    }
    else {
        self.nameLabel.textColor = [UIColor blackColor];
        self.colorLabel.textColor = [UIColor blackColor];
    }
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    [self updateCellDisplay];
}

- (void) setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    [self updateCellDisplay];
}

关于iphone - 更改自定义 UITableViewCell iphone 中的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2862193/

相关文章:

ios - 使用 NSTimer 的周期性本地通知不起作用

ios - Retina iPad 应用程序设计中的 DPI

ios - 如何检测是否支持 HTML5 自动播放属性?

iphone - 如何应用条件全屏播放视频和在特定帧播放

iphone - 如何从 uitableview 中的选定行填充标签字段

ios - 从 UITableView 的每个部分中选择的行(多项选择)

iphone - iOS7 使用 Storyboard 实现 UIViewControllerTransitioningDelegate

iphone - 密码字段的本地化和错误键盘

iphone - 如何将图标添加到 iPhone 应用程序?

ios - 快速删除时,使用 Realm 的 Tableview 不会重新加载