未声明“ indexPath”此功能中的第一次使用

问题描述

| 我有以下错误。 \'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];
}
我认为这是因为我没有将indexPath值从
didSelectRowAtIndexPath
解析为
pushDetailView
,但是我不知道该如何处理。 有人可以请教吗? 谢谢。     

解决方法

        问题是您的
pushDetailView:
方法的作用域没有
indexPath
变量。 代替
- (void)pushDetailView:(UITableView *)tableView {
您应该使您的方法像这样:
- (void)pushDetailView:(UITableView *)tableView andIndexPath: (NSIndexPath*) indexPath {
然后在方法范围内声明“ 5”。 然后,在您的
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:
,这是一篇不错的文章,解释了为什么有时更好地选择此方法     ,        由于您需要提供两个参数并在延迟包之后执行,所以将两个参数都放在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。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...