Cocoa touch 如何创建包含占据大部分单元格的单个UIButton的UITableViewCell';s空间?

Cocoa touch 如何创建包含占据大部分单元格的单个UIButton的UITableViewCell';s空间?,cocoa-touch,uitableview,uibutton,Cocoa Touch,Uitableview,Uibutton,我正在尝试创建一个包含单个大按钮的UITableViewCell 我尝试了看起来很明显的方法,在单元格的contentView中添加了一个UIButton,但没有成功(单元格显示为空)。我怎么了 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"startButtonCell"]; if (cell == nil) { cell = [[[UITableViewCell alloc] i

我正在尝试创建一个包含单个大按钮的UITableViewCell

我尝试了看起来很明显的方法,在单元格的contentView中添加了一个UIButton,但没有成功(单元格显示为空)。我怎么了

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"startButtonCell"];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:@"startButtonCell"] autorelease];
    UIButton *btn = [[UIButton alloc] initWithFrame:cell.contentView.bounds];
    [cell.contentView addSubview:btn];
    if (!self.task.isCompleted) {
        btn.titleLabel.text = @"Start!";
    }else{
        btn.titleLabel.text = @"Continue!";
    }
    [btn release];
}
请尝试以下操作:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:cell.contentView.frame];
[btn setTitle:@"Button Title" forState:UIControlStateNormal];
[cell.contentView addSubview:btn];
我目前还不是专家,但我认为您的解决方案的问题在于默认情况下UIButton的按钮样式是UIButtonTypeCustom,这是不可见的,因为它尚未定制。如果我把上面的代码从

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];


我得到了和你一样的结果。没有可见的按钮。如果您想使用UIButtonTypeCustom,您必须进行一些定制。例如,将背景图像添加到按钮。

我假设这是您的
-cellforrowatinexpath:
实现。。。如果(cell==nil){,您确定您的代码是在
之后执行的吗?按钮的大小是否正确创建?能否提供完整的
-cellforrowatinexpath:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];