我自己写了一个继承自UILabel的类
#import "RelatedLabel.h"
@implementation RelatedLabel
- (void)didMoveToSuperview {
[super didMoveToSuperview];
self.numberOfLines = 1;
self.font = [UIFont systemFontOfSize:12];
self.textColor = [UIColor colorWithWhite:0 alpha:0.54];
self.textAlignment = NSTextAlignmentLeft;
self.translatesAutoresizingMaskIntoConstraints = NO;
}
@end
然后添加到tableviewcell中
#import <UIKit/UIKit.h>
#import "RelatedLabel.h"
@interface HomePageTableViewCell : UITableViewCell
@property (nonatomic, strong) RelatedLabel *categoryLabel;
@end
#import "HomePageTableViewCell.h"
@implementation HomePageTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
_categoryLabel.text = @"category...";
[_categoryLabel sizeToFit];
[_categoryLabel setFrame:CGRectMake(16, 8, 100, 44)];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
但是这个label无法显示出来
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
文字颜色是白色的,是不是有可能显示出来了看不到呢…… view debug 看一看,颜色改成红色看一看
设置背景颜色 看看view的层级是否加了上去
awakeFromNib 这个函数你加载了吗,你通过nib方式创建了cell吗?
对象没创建加载啥,还没添加父视图,过于基础的问题了,楼主好好看看书吧。