Ricardo Sanchez-Saez
Ricardo Sanchez-Saez

Reputation: 9566

Make UITableViewCell behave like a real button

I want to make UITableViewCell to behave like real button.

Until know I have been using the

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath` 

trick, but this is not optimal because it doesn't behave like a button on tap/drag/release.

If you tap a cell row and drag your finger over the cell, it will not get selected when you release your finger (but a button would launch its action in the same case).

Is there any simple way of making a UITableViewCell to behave like a real button without resorting to insert an actual UIButton inside the cell?

Upvotes: 0

Views: 1107

Answers (3)

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

One way is to create a UIButtton of size of your cell and added it to the cell.

Or else you could simply add a UITapGestureRecognizer to your UITableViewCell and that will do the work for you.

Upvotes: 0

Nathan Day
Nathan Day

Reputation: 6037

You can just create table view cells with a button in them, set the buttons tag to the row so you can workout which row the button belongs to when you receive the button event. Just make sure you reset the buttons tag when you return a reused table view cell instead of creating a new one.

Upvotes: 1

Eugene
Eugene

Reputation: 10045

Subclass the UITableViewCell and use the following method:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
  [super setHighlighted:highlighted animated:animated];
  if (highlighted) {
    _backgroundImageView.image = [UIImage imageNamed:@"img_h"];
  }
  else {
    _backgroundImageView.image = [UIImage imageNamed:@"img"];
  }
}

Where img is a plain image and img_h is a highlighted version of that image.

Upvotes: 0

Related Questions