我有一个UITableCell的子类,到目前为止这是我的代码:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
voteCount = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2, 10, 10, self.frame.size.height)];
voteCount.text = @"24";
[self.contentView addSubview:voteCount];
upVote = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width/3, 0, 20 , self.frame.size.height)];
upVote.titleLabel.text = @"vote";
[self.contentView addSubview:upVote];
}
return self;
}
标签显示但按钮没有。我做错了什么?
答案 0 :(得分:2)
使用以下代码..
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
//For UILabel
voteCount = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width/2, 10, 10, self.frame.size.height)];
voteCount.text = @"24";
[self.contentView addSubview:voteCount];
//For UIButton
upVote = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[upVote addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[upVote setTitle:@"Title" forState:UIControlStateNormal];
upVote.frame = CGRectMake(20.0, 10.0, 60.0, 60.0);//set your coordinates
[self.contentView addSubview:upVote];
}
return self;
}
它会起作用..
答案 1 :(得分:1)
您需要使用[UIButton +buttonWithType:]
。
您还需要使用[UIButton -setTitle:forState:]
,而不仅仅是设置titleLabel
的文字。