【问题标题】:iPhone: didSelectRowAtIndexPath not invokediPhone:没有调用 didSelectRowAtIndexPath
【发布时间】:2010-02-21 12:01:22
【问题描述】:

我知道之前提到过这个问题,但那里的解决方案不适用。我有一个使用 IB 设置的带有嵌入式 UITableViewController 的 UINavigationController。在 IB 中,UITableView 的委托和数据源都设置为我派生的 UITableViewController。此类已使用 XCode 的 UITableViewController 类模板添加。没有自定义 UITableViewCell 并且表格视图仅使用带有单个标题的默认普通样式。

好吧,在模拟器中,列表正确呈现,两个元素由 dataSource 提供,因此 dataSource 正确链接。如果我在 IB 中删除 dataSource 的出口链接,则会呈现一个空表。

只要我点击这两个项目之一,它就会闪烁蓝色,并且 GDB 在__forwarding__ 范围内遇到UITableView::_selectRowAtIndexPath 的中断。它没有达到在我的非空方法 didSelectRowIndexPath 中设置的断点。我检查了参数和方法的名称以排除导致不同选择器的拼写错误。

我最近未能成功设置委托是否正确,但由于它设置为等效于从同一类获取两个元素的 dataSource,我希望它设置正确。那么,怎么了?

我正在运行 iPhone/iPad SDK 3.1.2 ...但也尝试在模拟器中使用 iPhone SDK 3.1。

编辑:这是我的 UITableViewController 派生的代码:

#import "LocalBrowserListController.h"
#import "InstrumentDescriptor.h"

@implementation LocalBrowserListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self listLocalInstruments];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [entries count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if ( ( [entries count] > 0 ) && ( [indexPath length] > 0 ) )
        cell.textLabel.text = [[[entries objectAtIndex:[indexPath indexAtPosition:[indexPath length] - 1]] label] retain];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ( ( [entries count] > 0 ) && ( [indexPath length] > 0 ) )
    {
        ...
    }
}

- (void)dealloc {
    [super dealloc];
}

- (void) listLocalInstruments {
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:10];

    [result addObject:[InstrumentDescriptor descriptorOn:[[NSBundle mainBundle] pathForResource:@"example" ofType:@"idl"] withLabel:@"Default 1"]];
    [result addObject:[InstrumentDescriptor descriptorOn:[[NSBundle mainBundle] pathForResource:@"example" ofType:@"xml"] withLabel:@"Default 2"]];

    [entries release];
    entries = [[NSArray alloc] initWithArray:result];
}

@end

【问题讨论】:

标签: iphone uitableview


【解决方案1】:

Apple 的文档说 didSelectRowAtIndexPath:index 在调用 selectRowAtIndexPath:indexPath 时不会被调用。要调用didSelectRowAtIndexPath,请使用以下命令:

[[tableView delegate] tableView:tableView didSelectRowAtIndexPath:index];

这基本上是调用委托。

【讨论】:

  • 太忘恩负义了! StackOverflow 的一大优点是它可以帮助社区,而不仅仅是个人。就个人而言,我发现这个回应正是我所需要的。感谢 RPM。
  • @soletan 我认为我的答案应该被选为接受的答案。只是说';-)
  • 这个答案清楚地解释了原因并解决了我的问题。谢谢!
  • 可能值得一提的是,如果您打算在委托方法中使用所选行的 NSIndexPath,则应在使用此代码之前调用selectRowAtIndexPath - 否则实际上不会选择任何单元格并且您得到零错误:)
【解决方案2】:

试试 didSelectRowAtIndexPath。您键入时的选择器名称中缺少“At”一词。

你在打电话吗


- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition

如果是,则不会调用委托方法 tableView:willSelectRowAtIndexPath: 或 tableView:didSelectRowAtIndexPath: 您必须自己调用它们。

请参阅UITableView Reference

我没有看到 3.0.x 或 3.1.x 版本中的表视图行为有任何差异。

【讨论】:

  • 感谢您的回复。抱歉打错了,当然该方法包括'At'......而且我没有以编程方式调用selectRowAtIndexPath()......它都是对用户的点击做出反应。我阅读了关于创建它们和管理选择的大部分 UITableView 文档。上面附加了我的派生控制器类的代码。
  • viewDidLoad 中以及在从tableView:cellForRowAtIndexPath: 返回之前检查代理是否匹配self 之前的集成代码,例如if ( [(UITableView *)self.view delegate] != self ) { ... }。此外,在 iPad 模拟器中,点击现有项目不会中断调试器,但在不输入 didSelectRowAtIndexPath 的情况下选择项目...看起来 XCode 在编辑文档第 100 页时的行为类似于 Word 97。
【解决方案3】:

好吧,在尝试保留 UITableView 实例的委托只是为了检查内存泄漏是否成功之后,我调查了这个问题并偶然发现了一些关于如何使用分离的 NIB 组合视图和控制器的教程,就像我在这里所做的那样。这个教程终于让我成功了:

Combining View Controllers

详细关注错误,有两个 UITableViewControllers ... 一个在主 NIB 中设置为选项卡式导航控制器的根控制器,第二个在引用的 NIB 中用于提供原始 UITableView 实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2023-04-01
    • 2012-04-06
    • 2015-05-17
    • 2015-02-22
    相关资源
    最近更新 更多