我正在尝试创建一个包含一个大按钮的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];
}
答案 0 :(得分:2)
尝试以下方法:
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];
到
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
我得到了与你相同的结果。没有按钮可见。如果您想使用UIButtonTypeCustom,您可以进行一些自定义。例如,将背景图像添加到按钮。