'indexPath'未声明(首次使用此函数)

时间:2011-06-15 12:48:57

标签: iphone uitableview uiview didselectrowatindexpath nsindexpath

我有以下错误。 'indexPath'未声明(首次使用此功能)

代码即可。 的 didSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {  

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];
cell.accessoryView = spinner;
[spinner startAnimating];
[spinner release];

[self performSelector:@selector(pushDetailView:) withObject:tableView afterDelay:0.1];
}

pushDetailView

- (void)pushDetailView:(UITableView *)tableView {

// Push the detail view here
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//load the clicked cell.
DetailsImageCell *cell = (DetailsImageCell *)[tableView cellForRowAtIndexPath:indexPath];

//init the controller.
AlertsDetailsView *controller = nil;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
controller = [[AlertsDetailsView alloc] initWithNibName:@"DetailsView_iPad" bundle:nil];
} else {
controller = [[AlertsDetailsView alloc] initWithNibName:@"DetailsView" bundle:nil];
}

//set the ID and call JSON in the controller.
[controller setID:[cell getID]];

//show the view.
[self.navigationController pushViewController:controller animated:YES];
}

我认为这是因为我没有将didSelectRowAtIndexPath的indexPath值解析为pushDetailView,但我不知道如何解决这个问题。

有人可以提出建议吗?

感谢。

2 个答案:

答案 0 :(得分:3)

问题是你pushDetailView:方法的范围没有indexPath变量。

而不是

- (void)pushDetailView:(UITableView *)tableView {

你应该像这样制作你的方法:

- (void)pushDetailView:(UITableView *)tableView andIndexPath: (NSIndexPath*) indexPath {

然后indexPath将在方法范围内声明。

然后,在didSelectRowAtIndexPath方法中,替换

[self performSelector:@selector(pushDetailView:) withObject:tableView afterDelay:0.1];

代码如下:

double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [self pushDetailView: tableView andIndexPath: indexPath];
});

这会在延迟后使用GCD执行代码,而不是performSelector: withObject :afterDelay:,而a nice post解释为什么有时候选择这种方法会更好

答案 1 :(得分:0)

由于你需要提供两个参数并在延迟后执行,然后在NSDictionary中打包两个参数并传递它:

    NSDictionary *arguments = [NSDictionary dictionaryWithObjectsAndKeys:
    tableView, @"tableView", indexPath, @"indexPath", nil];
    [self performSelector:@selector(pushDetailView:) withObject:arguments afterDelay:0.1];
    ...

- (void)pushDetailView:(NSDictionary *)arguments {
    UITableView *tableView = [arguments objectForKey:@"tableView"];
    NSIndexPath *indexPath = [arguments objectForKey:@"indexPath"];
    ...

或者@Felipe建议,使用GCD。