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 の値をdidSelectRowAtIndexPathtoから解析していないためだと思います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

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 を使用します。

于 2011-06-15T13:15:49.187 に答える