问题描述
|
我有以下错误。 \'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。