views:

6323

answers:

9

I'd like one of my table rows to be a button that takes up an entire row of my UITableView. I figured the best way to go about this is to instantiate a UIButton, and give it the same frame size as an instance of UITableViewCell, and add that as a subview to the cell. I'm almost there, but quite a few pixels off to not get that perfect touch to it. Is there a better approach to this, or perhapsps can my placement accuracy be fixed up to get that perfect alignment?

cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
    cell = [self tableviewCellWithReuseIdentifier:CellIdentifier];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0.0f, 5.0f, 320.0f, 44.0f)];

[button setTitle:@"Do Stuff" forState:UIControlStateNormal];
[button addTarget:self action:@selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
+3  A: 

You can fake it in your didSelectRowAtIndexPath: method. Have the tableView cell act as the button.

[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES];
[self doStuff];
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];

Use setSelected: animated: to fake a button press and have it fade. Might do the trick for you.

Ryan Townshend
I like this idea. Works great with grouped tableviews.
Markus Müller
A: 

This may be beside the point, but your code may have the problem where another button is added to the cell every time the cell is re-used.

Chris Lundie
A: 

I had the same problem as you. And Ryan gave you the answer: use the cell itself as a button with the didSelectedRowAtIndexPath: method.

Geraud.ch
A: 

You can also add add a UIImage to your UITableViewCell to make the cell appear to be a button.

drewh
+1  A: 

You can also create a view with a button and place it in the footer of the section above it. That way you don't need to worry about the cell image in the background. I've done this to place two half-width buttons side-by-side in a grouped table view.

Dave Batton
A: 

FWIW, I put the button in my app at CGRectMake(9, 0, 302, 45) and I think it looks perfect.

A: 

Class(UITableViewCell) has contentView property. So I put basic button in contentView of UITableViewCell variable.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 ・・・
 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
 btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
 [btn setTitle:@"OK" forState:UIControlStateNormal];
 [btn setTitle:@"OK" forState:UIControlStateSelected];
 [btn addTarget:self action:@selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
 [cell.contentView addSubview:btn];
 ・・・
 return cell;
}

Sorry, poor my English.

A: 
UIButton *btnView = [[UIButton alloc] init];
btnView.frame = CGRectMake(52.0f, 2.0f, 215.0f, 38.0f);
[btnView setImage:[UIImage imageNamed:@"invite.png"] forState:UIControlStateNormal];
[btnView addTarget:self action:@selector(showInviteByMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btnView];
[btnView release];
Slim
A: 

if you want it to fit perfectly, just call the cell's bounds as your CGRect. do it like this:

button.frame = cell.bounds;

That should get your button to be exactly the same as your cell. Hope that helps!

Cord

Cord LaBarre