'索引路径'未声明(在此函数中首次使用)

发布于 2024-11-15 16:24:43 字数 1573 浏览 6 评论 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];
}

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

有人可以建议吗?

谢谢。

I have the following error. 'indexPath' undeclared (first use in this function).

Code.
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];
}

I think it's because I'm not parsing the indexPath value from didSelectRowAtIndexPath to pushDetailView but I don't know how to approach this.

Could someone please advise?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作妖 2024-11-22 16:24:43

问题是您的 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: 这里是 一篇不错的文章解释了为什么有时最好选择这种方法

The problem is that you pushDetailView: method has no indexPath variable on it's scope.

Instead of

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

You should make your method like this:

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

and then indexPath would be declared on the method scope.

Then, inside your didSelectRowAtIndexPath method, replace

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

for the code bellow:

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];
});

This uses GCD to execute the code after a delay, instead of the performSelector: withObject :afterDelay: and here is a nice post explaining why sometimes is better to opt for this aproach

不知所踪 2024-11-22 16:24:43

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

Since you need to provide two arguments and perform after a delay package both arguments in an NSDictionary and pass it:

    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"];
    ...

Or as @Felipe suggests, use GCD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文