我有一个带有 UITableView 的 View Controller 。我将数据源和委托(delegate)都设置为 View Controller 。方法 cellForRowAtIndexPath: 被调用并按预期运行,但 didSelectRowAtIndexPath: 未被调用。有什么想法吗?
我的 .h 中有这个:
@interface DetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
在 .m 的 viewDidLoad 中,我有:
self.tableView.dataSource=self;
self.tableView.delegate=self;
调用 cellForRowAtIndexPath 但不调用 didSelectRowAtIndexPath 似乎没有意义。这是该方法现在的样子:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"did select row at index path");
}
最佳答案
我遇到过一些可能的原因:
确保 tableView 的编辑属性设置为 NO。当表的编辑属性设置为 YES 时,不会调用 didSelectRowAtIndex: 方法。默认情况下,此属性设置为 NO。
self.tableView.editing = NO;
确保 allowsSelection 属性设置为 YES。默认情况下是。
尝试在 viewDidLoad 方法中调用.[super viewDidLoad] 或在初始化初始化程序中设置委托(delegate)和数据源属性。
在loadView 方法中设置delegate 和dataSource 属性,并将tableView 分配给UIViewController 的view 属性。这是一个例子:
- (void)loadView { UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain]; tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; tableView.delegate = self; tableView.dataSource = self; [tableView reloadData]; self.view = tableView; }
(实际上,这就是我通常做的) 尝试从继承自 UIViewController 的 UITableViewController 继承 DetailViewController。 UITableViewController 自动符合 UITableViewDelegate 和 UITableViewDataSource。
关于ios - UITableView didSelectRowAtIndexPath 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34030387/