iPhone:didSelectRowAtIndexPath未被调用

sol*_*tan 12 iphone uitableview

我知道之前提到的这个问题,但那里的决议并不适用.我有一个UINavigationController,它使用IB设置嵌入式UITableViewController.在IB中,UITableView的委托和dataSource都设置为我的UITableViewController的派生.使用XCode的UITableViewController类模板添加了这个类.没有自定义UITableViewCell,表视图仅使用具有单个标题的默认普通样式.

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

一旦我点击这两个项目中的一个,它就会闪烁蓝色并且GDB __forwarding__在a的范围内遇到中断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
Run Code Online (Sandbox Code Playgroud)

RPM*_*RPM 91

Apple的文档说,didSelectRowAtIndexPath:index调用时不会调用selectRowAtIndexPath:indexPath它.要打电话请didSelectRowAtIndexPath使用以下内容:

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

这基本上调用了委托.

  • 那是相当忘恩负义的!StackOverflow的一大优点是帮助社区,而不仅仅是个人.就个人而言,我发现这种反应正是我所需要的.谢谢RPM. (13认同)

sol*_*tan 2

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

组合视图控制器

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