Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton to cover a UITableViewCell

Tags:

iphone

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];
like image 270
Coocoo4Cocoa Avatar asked Dec 05 '08 19:12

Coocoo4Cocoa


1 Answers

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.

like image 181
Ryan Townshend Avatar answered Sep 21 '22 17:09

Ryan Townshend