0

我有一个 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;
}

标签显示,但按钮不显示。我究竟做错了什么?

4

2 回答 2

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;
    }

它会工作..

于 2013-08-25T06:42:07.693 回答
1

你需要使用[UIButton +buttonWithType:].

你还需要使用[UIButton -setTitle:forState:],而不是仅仅设置它的文本titleLabel

于 2013-08-25T06:37:00.503 回答