I’m trying to create a UITableViewCell that contains a single big button.
I tried what seemed obvious, adding a UIButton to the cell’s contentView, but it didn’t work (the cell is displayed empty). What am I dong wrong?
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];
}
Try the following:
I am by far no expert but I think the problem with your solution is that by default the button style of UIButton is UIButtonTypeCustom, which is invisible because it is not customized yet. If I change the code above from
to
I get get the same result you do. No Button visible. If you would like to use UIButtonTypeCustom you would have do do some customization. Adding a background image to your button for example.