【问题标题】:'indexPath' undeclared (first use in this function)'indexPath' 未声明(在此函数中首次使用)
【发布时间】:2011-06-15 12:48:56
【问题描述】:

我有以下错误。 '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,但我不知道如何处理。

有人可以建议吗?

谢谢。

【问题讨论】:

    标签: iphone uitableview uiview didselectrowatindexpath nsindexpath


    【解决方案1】:

    问题是你的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 解释为什么有时选择这种方法更好

    【讨论】:

    • 酷...所以我可以同时使用- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 和- (void)pushDetailView:(UITableView *)tableView andIndexPath: (NSIndexPath*) indexPath?
    • “同时”是什么意思?
    • 嗯...我编辑了代码,解释了您应该做什么...如果您有任何疑问,请告诉我:)
    • @Felipe:它正在成功推送到新视图。谢谢。但是有一个问题;当我转到下一个视图并再次返回时,我单击的单元格上的箭头消失了。为什么?此外,还有 2 个警告。第一个在:[self pushDetailView: tableView andIndexPath: indexPath];。 -> 方法'-pushDetailView:andIndexPath.'未找到(返回类型默认为“id”)。第二个是:- (void)pushDetailView:(UITableView *)tableView andIndexPath: (NSIndexPath*) indexPath。 -> “警报”可能不会响应“-pushDetailView:andIndexPath”。你知道为什么吗?非常感谢。
    • 我猜箭头消失了,因为你用 spinner 对象替换它(在你的附件视图中)。并且警告可能是因为您尚未将方法新签名更新到 .h 文件中,然后编译器认为此方法可能不存在
    【解决方案2】:

    由于您需要提供两个参数并在延迟后将两个参数打包到一个 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。

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 2014-04-22
      • 2012-05-07
      • 2012-07-13
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多