newguy
newguy

Reputation: 5986

Displaying a UIButton on UITableView cell

I want to display a customized button with "!" as its text inside a UITableView cell. The code is like this:

UIButton *informationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[informationBtn setFrame:CGRectMake(10, 5, 55, 55)];
[informationBtn setTitle:@"!" forState:UIControlStateNormal];
[informationBtn addTarget:self action:@selector(popUpWindowWithMessage:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:informationBtn];

where "cell" is defined as a UITableViewCell instance.

But the button doesn't appear on the cell. What should I do to display the button?

Upvotes: 1

Views: 783

Answers (2)

iOS_Developer
iOS_Developer

Reputation: 183

Your code is correct, And it seems the button is there. But you can't see it. Change the color of button title. other than white. then check..

[informationBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

Add the above line after setting title of your button.

Upvotes: 4

WrightsCS
WrightsCS

Reputation: 50727

If you are adding the button to the cells accessoryView, you can do this:

[cell setAccessoryView: informationBtn];

Upvotes: 1

Related Questions