Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell, button in cell not working

I have a UITableViewController where I'm working with a custom UITableViewCell. The cell contains an image, labels and buttons. Everything is working as expected except of the buttons. When pressing the button, nothing happens. Only the cell gets selected.

I made a connection in IB from the button to my custom TweetTableViewCell class.

@property (weak, nonatomic) IBOutlet UIButton *cellFollowButton;
- (IBAction)followButtonTappedonCell:(id)sender;

I'm writing some text to the console in my followButtonTappedonCell action. But when pressing the button, nothing happens. It seems to me that some view consumes the tap before it arrives to the button. To make it more confusing... When I add a button in may UITableViewController programatically like this:

[cell.cellFollowButton addTarget:self action:@selector(customActionPressed) forControlEvents:UIControlEventTouchDown];
[cell addSubview:cell.cellFollowButton];

...it just works. I read in other articles that disabling "User Interaction Enabled" solved similar problems, but it doesn't in my case. I'm sure that many others must have run into this problem when using buttons in UITableViewCells. Any help is highly appreciated.

I'm using Xcode 5 for iOS 7.

like image 853
Oliver Schobel Avatar asked Jan 31 '14 15:01

Oliver Schobel


2 Answers

If your cell is in a nib, make sure the root object is a UITableViewCell and NOT a UIView

nib with UITableViewCell as root

like image 123
Adriano Spadoni Avatar answered Sep 25 '22 22:09

Adriano Spadoni


I would recommend you to go through this link. It has clear description of your problem. However, I've tried to extract needed info.

Many types of events rely on a responder chain for event delivery. The responder chain is a series of linked responder objects. It starts with the first responder and ends with the application object. If the first responder cannot handle an event, it forwards the event to the next responder in the responder chain.

A responder object is an object that can respond to and handle events. The UIResponder class is the base class for all responder objects, and it defines the programmatic interface not only for event handling but also for common responder behavior. Instances of the UIApplication, UIViewController, and UIView classes are responders, which means that all views and most key controller objects are responders.

In your Case, when you are writing event methods in your customTableViewCell class, your methods are not being called. this is because, TableViewCell is not participating in Responder chain.

next thing is, check whether "User Interaction is enabled " or not for your Cell. You can find it under View section of "Attribute inspector".

like image 27
Prince Agrawal Avatar answered Sep 24 '22 22:09

Prince Agrawal