-1

我面临一个问题,没有调用 didselectrowatindexpath。我的tableview在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 。但这里不是这种情况。现在我的问题出在我的拆分视图控制器中,每当我选择任何选项时,相应的详细视图都不会显示并且是空白的。我在 rootview 控制器(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 的指南:Table View Programming Guide

于 2013-02-19T05:17:11.740 回答
0

由于您使用 LoadMenuItems 的方式,您的程序可能会卡在长时间运行的操作中。每次调用 numberOfRowsInSection 和 cellForRowAtIndexPath 时,您都在重新创建整个数组——这很糟糕。您应该调用一次 LoadMenuItems,也许在 viewDidLoad 中,一旦创建了该数组,就使用它,而不是在需要获取数据时调用 LoadMenuItems。我不知道这是否是导致您的问题的原因,但您绝对应该修复它,看看它是否有所作为。

于 2013-02-19T05:46:05.640 回答