Gary Wright
Gary Wright

Reputation: 2469

Unable to select rows of UITableView in UITableViewController

I have created an app using a storyboard. In other screens, I have used a UITableViewController directly and the selection is working as expected.

In this case, I have a UITableView that is one of several controls within a UIViewController.

My custom ViewController.h file has a definition similar to the below:

@interface MyViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
    @property (weak,nonatomic) IBOutlet UITableView *myTableView;
@end

Then within viewDidLoad I am doing this:

_myTableView.delegate = self;
_myTableView.dataSource = self;

Having done this, my numberOfSectionsInTableView, numberOfRowsInSection and cellForRowAtIndexPath methods are all being called and my table looks as I want it to.

The problem I have is that the rows do not select and the didSelectRowAtIndexPath method is not getting called.

I have checked that Selection is set to Single Selection in the storyboard view and I have also tried to set _myTableView.allowsSelection=YES; in viewDidLoad but this doesn't seem to make any difference.

I know that this is probably something to do with the fact that my table is within a normal view controller, but I can't figure out the magic step I've missed to make the selection work.

For now I have added a workaround. In cellForRowAtIndexPath I have attached a UITapGestureRecognizer to each view in the cell:

for(UIView *view in cell.contentView.subviews){
    [view setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRow:)];
    [tap setNumberOfTapsRequired:1];
    [view addGestureRecognizer:tap];
    view.tag = row; // So that I can identify in the handler which row has been tapped
}

Then in the handler:

-(void)tapRow:(id)sender{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer*)sender;
    UIView *myView = (UIView*)gesture.view;
    int row = myView.tag;
    // Handle tap of row here
}

This achieves what I need, but I would still like to figure out what I've done wrong with the row selection!

Upvotes: 1

Views: 1811

Answers (5)

Samuel Gabriel
Samuel Gabriel

Reputation: 11

Just incase anyone still has this problem. I just realised -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method won't work for any view or subview with a tap gesture recogniser set to it. So remove the tap gesture recogniser from that view of it's parent view. if you still want to handle touch events, then use "touchesBegan" function. ` - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event];

UITouch *touch = [touches anyObject];

}`

Upvotes: 1

Adela Toderici
Adela Toderici

Reputation: 1112

I had the exactly the same problem with my current project for two minutes yesterday when I realised that I didn't link my delegate and dataSource of the table view. You made it programmatically, so it should work. For me was a delegation problem, you can check your Attributes Inspector to make sure that everything is ok.

Upvotes: 0

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

We type sometime wrong delegate call didDeselectRowAtIndexPath instead of didSeselectRowAtIndexPath. Is it your case?

I see that you can update your content in the tableview so the connection should be OK. I think about mistakes with function names.

Just to test: Replace your didSelectRowAtIndexPath function with this (copy, paste).

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Selected %d",indexPath.row);
}

Upvotes: 1

Mihawk
Mihawk

Reputation: 589

In cellForRowAtIndexPath method add this line

cell.userInteractionEnabled = NO;

Upvotes: -3

Brian
Brian

Reputation: 15706

Make sure there is not another view on top of the table view (e.g. a transparent view that would be stealing touch events). Also ensure that userInteraction is enabled for the tableview and all of its parent views.

Upvotes: 1

Related Questions