问题
I have a UITableView
inside a UIViewController
that adopts UITableViewDelegate
& UITableViewDataSource
protocols. numberOfRowsInSection
and cellForRowAtIndexPath
are implemented.
There are outlets for dataSource & delegate in storyboard. Single selection is chosen for TableView. Show selection on touch is checked.
I run project on the simulator, touch table cell, got didHighlightRowAtIndexPath
call, but didSelectRowAtIndexPath
or willSelectRowAtIndexPath
are never called.
What did I forgot? What can affect TableView in this way?
P.S. I know, there is a plenty of questions like this, but I've spent a few hours googling and reading stackoverflow and still didn't solve my problem.
TestViewController.h:
@interface TestViewController : ViewController <UITableViewDelegate, UITableViewDataSource>
@end
TestViewController.m:
@interface TestViewController ()
@property (strong) IBOutlet UITableView *tableView;
@end
@implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"selected");
}
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"highlighted");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FilterCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = @"some text";
return cell;
}
@end
回答1:
Hard to say without seeing code, but try this:
1) The didSelectRowAtIndexPath
is from UITableViewDelegate
, NOT dataSource, do you have it connected?!
2) Check the signature of didSelectRowAtIndexPath
carefully. Copy paste it from some trusted source. Maybe some mistype there.
3) Be sure that outlets are connected properly. Stop it somewhere with a debugger, say viewDidLoad
and print dataSource & delegate (po self.tableView.dataSource
debugger command), make sure they point to the correct object.
4) Does the row get selected visually? Maybe you have cell.userInteractionEnabled
set to NO in code or storyboard?
5) Table view has a selection property in storyboard: (Is it ok?)

6) Maybe you have table in editing mode? Log it from debug to see.
7) Does the row get's selected from point of view of indexPathsForSelectedRows
method? What if you override and log calls to cell's selected
accessor?
回答2:
I've got it. Problem was in a library that project uses.
My ViewController was inherited from a library's ViewController implementation and it was catching all the gestures.
来源:https://stackoverflow.com/questions/20020113/uitableview-didselectrowatindexpath-never-called