I have a regular UIViewController to which I have added a UITableView inviewDidLoad, like this:

dataTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 250, 320, 318)];
dataTableView.dataSource = self;

[self.view addSubview:dataTableView];

It's working fine, the cell contents are there, which I added via the call to -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. I can scroll and everything, but I can't get the cells to react to touch/click. When I press a cell, it stays selected, but I can't seem to attach an action to it. The code I'm using is

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"select");
}

But it never fires. Do I have to make some sort of connection between the tableview and the call? If so, why does it work for cellForRowAtIndexPath without problem? Any help is appreciated. I'm sure this is simple to fix for somebody who knows their "Objective-C"

有帮助吗?

解决方案

You didn't set yourself up as the delegate of the dataTableView.

dataTableView.delegate = self;

The reason that cellForRowAtIndexPath works is because that's part of the dataSource whereas didSelectRowAtIndexPath is part of the delegate.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top