我有一个自定义单元格,当用户选择该单元格时,我希望两个 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/