golddove
golddove

Reputation: 1206

Call didSelectRowAtIndexPath only for a part of the UITableViewCell

I have a UITableView populated with many custom UITableViewCells. Each of them has a colored rectangle as a subview (currently it is a stylized UIButton but I can implement it anyway necessary).

I want the UITableViewCell to call tableview:didSelectRowAtIndexPath: only when that part is selected.

Other similar questions asked how to get subviews to ignore events. But this would not work for me because selecting anywhere outside that rectangle would also trigger it.

I am not sure whether to limit the area of the cell that triggers the method or to disable triggering from the cell and instead allow the subview to trigger the method. Either way, I do not know the details of the implementation.

Upvotes: 2

Views: 2514

Answers (4)

Hayden Kopser
Hayden Kopser

Reputation: 1

Have you considered not calling didSelectRowAtIndexPath and instead creating an action for the UIButton to trigger when it is touched? I am not sure exactly what you want to do when when the UIButton is touched but lets say you just wanted to push a new view controller within the method.

If you are using a nib or storyboard and your UITableView instance belongs to a UITableViewController you could connect your UIButton to an action that looks like this,

-(IBAction)buttonTapped:(id)sender {

     NewViewController *newViewController = [[NewViewController alloc] init];
     [newViewController setSomeProperty:property];
     [[self navigationController] pushViewController:newViewController animated:YES];
}

If you are not using a nib or storyboard then you could just connect an action to the UIButton programmatically in your init method or elsewhere.

Upvotes: 0

Sam Fischer
Sam Fischer

Reputation: 1472

Make a custom subclass of a UITableViewCell and in that - add your own button. In whatever area you want.

Upvotes: -1

Andrew Hoos
Andrew Hoos

Reputation: 1520

I don't know why you want to call didSelectRowAtIndexPath: but if you want to receive touch events from the button you can connect the button pressed event(touch up inside) to the table view controller if you have subclassed it or connect it to the table view controller's delegate if you have delegated it. If you need to actually select the row then you can call selectRowAtIndexPath:animated:scrollPosition: and select the row programmatically when the button is pushed.

Upvotes: 1

William Falcon
William Falcon

Reputation: 9813

In didSelect get your cell (passed to you), and check to see if the button in that cell is selected. If it is then do what you need, otherwise ignore it.

Upvotes: 2

Related Questions