-1

didselectrowatindexpath が呼び出されないという問題に直面しています。私のテーブルビューは次のようにviewdidloadに設定されています:

- (void)viewDidLoad
{
    //[super viewDidLoad];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
    self.detailViewController = (klViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

}

今、私の cellForRowAtIndexPath が呼び出され、次のようにセルを再利用しています:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"menuSelection1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    AmbassadorInfoData * data = [self.LoadMenuItems objectAtIndex:indexPath.row];
    cell.textLabel.text = data.treeName;

    return cell;
}

私は他の回答を確認しましたが、ほとんどの場合、 didDeselectRowAtIndexPath が使用される可能性があると言いました。しかし、ここではそうではありません。対応する詳細ビューが表示されず、空白のオプションを選択するたびに、分割ビューコントローラーに問題があります。ルートビュー コントローラー (klViewController) のメソッドはどれも呼び出されません。この理由は何でしょうか?以下は、UITableView に関連するすべてのメソッドです。viewDidLoad と同じクラスに実装されています。

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.LoadMenuItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"menuSelection1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    AmbassadorInfoData * data = [self.LoadMenuItems objectAtIndex:indexPath.row];
    cell.textLabel.text = data.treeName;

    return cell;
}

-(NSArray *)LoadMenuItems
{


    NSMutableArray * menuData =  [[NSMutableArray alloc] initWithCapacity:5];
    AmbassadorInfoData * data = [[AmbassadorInfoData alloc] init];

    data.treeName = @"Ambassador Info";
    data.treeDescription = @"This is temp data.";
    NSString * file = [[NSBundle mainBundle] pathForResource:@"oak" ofType:@"jpg"];
    data.treePhoto = [UIImage imageWithContentsOfFile:file];

    [menuData addObject:data];
    data=nil;

    data = [[AmbassadorInfoData alloc] init];
    data.treeName = @"AmbassadorInternalList";
    data.treeDescription = @"This is temp data.).";
    file = [[NSBundle mainBundle] pathForResource:@"DouglasFir" ofType:@"jpg"];
    data.treePhoto = [UIImage imageWithContentsOfFile:file];
    [menuData addObject:data];
    data=nil;

    data = [[AmbassadorInfoData alloc] init];
    data.treeName = @"Text 3";
    data.treeDescription = @"This is temp data.";
    file = [[NSBundle mainBundle] pathForResource:@"SugarMaple" ofType:@"jpg"];
    data.treePhoto = [UIImage imageWithContentsOfFile:file];
    [menuData addObject:data];
    data=nil;

    data = [[AmbassadorInfoData alloc] init];
    data.treeName = @"Text 4";
    data.treeDescription = @"This is temp data.";
    file = [[NSBundle mainBundle] pathForResource:@"RedMaple" ofType:@"jpg"];
    data.treePhoto = [UIImage imageWithContentsOfFile:file];
    [menuData addObject:data];
    data=nil;

    data = [[AmbassadorInfoData alloc] init];
    data.treeName = @"Text 5";
    data.treeDescription = @"This is temp data.";
    file = [[NSBundle mainBundle] pathForResource:@"Pine" ofType:@"jpg"];
    data.treePhoto = [UIImage imageWithContentsOfFile:file];
    [menuData addObject:data];
    menuItems = [[NSArray alloc] initWithArray:(NSArray *)menuData];
    data=nil;

    return menuItems;


}




#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    AmbassadorInfoData * selectedItem =(AmbassadorInfoData *)[self.menuItems objectAtIndex:[self.tableView indexPathForSelectedRow].row];


    detailObject = selectedItem;

    [detailViewController setDetailItem:detailObject];


}

@end
4

2 に答える 2

0

次の変更を試してください。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"TableView methods are called! problem is getting AmbassadorInfoData object") 

    AmbassadorInfoData * selectedItem =(AmbassadorInfoData *)[self.menuItems objectAtIndex:indexPath.row];

    if (!selectedItem) NSLog(@"Unable to get Ambassador object, check your menuItems");

    [self.detailViewController setDetailItem:selectedItem];   //notice "self."


}

また、コメントを外します[super viewDidLoad];

編集:

あなたはあなたが理解していないコードを使用しているようです、この場合私はいくつかの基本を学ぶことを提案しています....これは私が物事をかなり何度も理解するのを助けた素晴らしいサイトです

これはAppleのガイドです:テーブルビュープログラミングガイド

于 2013-02-19T05:17:11.740 に答える
0

LoadMenuItems の使用方法が原因で、プログラムが実行時間の長い操作でスタックしている可能性があります。numberOfRowsInSection と cellForRowAtIndexPath が呼び出されるたびに、配列全体を再作成しています。これは悪いことです。おそらくviewDidLoadでLoadMenuItemsを1回呼び出す必要があり、その配列を作成したら、データを取得する必要があるときにLoadMenuItemsを呼び出すのではなく、それを使用します。これが問題の原因かどうかはわかりませんが、間違いなく修正して、違いが生じるかどうかを確認する必要があります.

于 2013-02-19T05:46:05.640 に答える