0

我有以下错误。'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];
}

我认为这是因为我没有从didSelectRowAtIndexPathto解析 indexPath 值,pushDetailView但我不知道如何处理这个问题。

有人可以建议吗?

谢谢。

4

2 回答 2

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:这里是一个很好的帖子,解释了为什么有时选择这种方法更好

于 2011-06-15T12:57:25.403 回答
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。

于 2011-06-15T13:15:49.187 回答