0

我是 iOS 新手。

我想使用 Today 扩展。我已经创建了 Today 扩展并添加了 uitableview 它工作正常。但是当我尝试选择行时,委托方法DidSelectRowAtIndexPath不会调用。

在我的代码中有 uitableview 委托和数据源的 3 种方法。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return 3;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

if (cell == nil) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
}

cell.textLabel.text = [NSString stringWithFormat:@"Hello World%li",(long)indexPath.row];
return cell;

}

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      return 1;
     }

以上数据源方法调用正确

但是在选择行后不会调用 didselect 行方法。

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

感谢您的帮助。

4

2 回答 2

1

您提到调用了数据源方法。但是tableView: didSelectRowAtIndexPath:是一种UITableViewDelegate方法,而不是数据源方法。看起来表格视图的delegate属性可能未设置。

于 2014-12-31T19:07:37.773 回答
0

在表视图类中实现 UITableViewDelegate 协议,与实现 UITableViewDataSource 相同。didSelectRowAtIndexPath可以正常工作,即在选择表中的一行时调用。

于 2016-10-07T07:20:01.553 回答