自定义单元格.h
@interface CustomCell : UITableViewCell
@property (nonatomic, strong) UILabel *labelUsername;
@property (nonatomic, strong) UIView *circle;
@property (nonatomic, strong) UILabel *labelFirstName;
@property (nonatomic, strong) UILabel *labelLastName;
@end
自定义单元格.m
@implementation CustomCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.circle = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 40.0f, 40.0f)];
[self.circle setBackgroundColor:[UIColor brownColor];
self.labelUsername = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 200.0f, 50.0f)];
self.labelUsername.textColor = [UIColor blackColor];
[self.contentView addSubview:self.labelUsername];
[self.contentView addSubview:self.circle];
}
return self;
}
表视图.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customCellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:customCellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inviteCellIdentifier];
}
cell.labelFirstName.text = @"FirstName";
cell.labelLastName.text = @"LastName";
return cell;
}
上面的代码继续显示
labelUsername
和
circle
。但是,使用IB创建的财产(
labelFirstName
和
labelLastName
)没有出现。
所以在tableView.m中
viewDidLoad
我使用以下代码注册了Nib:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:@"CustomCell"];
}
现在
标签名字
和
标签姓氏
出现,但使用代码创建的财产(
label用户名
和
圆圈
)不要出现。
我如何才能让所有4个财产都显示出来?