如何根据值在prepareforSegue中显示数组中的条目?

时间:2014-02-10 06:17:25

标签: ios objective-c arrays xcode

我有一个包含对象的数组,这些对象显示在TableViewController中,每行都有一些字段,如team idnamepoints。< / p>

现在我创建了一个包含更多对象/属性的additional team info数组 - 完整的团队名称,基础年份和团队ID 等。

我想要的是,当用户点击我的TableViewController中的某一行时,它会读取团队ID并且在prepareforSegue中有一些方法可以搜索我的additional team info数组并查找匹配团队ID并在另一个segue中显示该对象数据。以下是我的一些代码:

NSMutableArray *allInfo = [[NSMutableArray alloc]init];

for(int i = 0; i < 14; i++) {

    ExtraTeamInfoObject *temp = [[ExtraTeamInfoObject alloc] init];

    temp.teamFullNames = _teamFullNames[i];
    temp.teamId = _teamId[i];
    temp.teamStadiumNames = _teamStadiumNames[i];
    temp.stadiumCapacity = _stadiumCapacity[i];
    temp.clubFoundationDate = _clubFoundationDate[i];
    temp.stadiumBuiltYear = _stadiumBuiltYear[i];
    temp.teamCity = _teamCity[i];
    temp.clubPresident = _clubPresident[i];
    temp.headCoach = _headCoach[i];
    temp.championshipsWon = _championshipsWon[i];
    temp.domesticCupsWon = _domesticCupsWon[i];
    temp.domesticLeagueCupsWon = _domesticLeagueCupsWon[i];
    temp.domesticSuperCupsWon = _domesticSuperCupsWon[i];
    temp.championsleaguesWon = _championshipsWon[i];
    temp.europaleaguesWon = _europaleaguesWon[i];
    temp.europeanSuperCupsWon = _europeanSuperCupsWon[i];
    temp.worldclubchampionshipsWon = _worldclubchampionshipsWon[i];

    [allInfo addObject:temp];
}

for (int i = 0; i < 14; i++) {
    NSLog(@"Show me the goods %@", [allInfo[i] teamFullNames]); // just making sure the objects are being created correctly.
    }
}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([[segue identifier] isEqualToString:@"teamDetailsSeg"]){

        TeamDetailsTableViewController *detailViewController = [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
        long row = [myIndexPath row];

        //detailViewController.teamDetailModel = before I would just type the array here like _teamFullName[row] and it would give me data i needed

    }
}

1 个答案:

答案 0 :(得分:0)

我会做这样的事情:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"teamDetailsSeg"] && [segue.destinationViewController isKindOfClass:[TeamDetailsTableViewController Class]){

        TeamDetailsTableViewController *detailViewController = [segue destinationViewController];

        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
        long row = [myIndexPath row];


        //detailViewController.teamDetailModel =  before i would just type the array here like _teamFullName[row] and it would give me data i needed

        NSNumber *teamID = _teamFullName[row];

        for (ExtraTeamInfoObject *item in allInfo) {
            if ([item.teamID integerValue] == [teamID integerValue]) {
                detailViewController.extraInfo = allInfo[allInfo indexOfObject:item];
                break;
            }
        }
    }
}

但我同意vokilam。 NSDictionary是更好的选择。

修改

类似的东西:

self.teamArray = [[NSMutableArray alloc] init]; // assume you have @property (nonatomic, strong) NSMutableArray *teamArray;

NSString *teamID = @"123456";
NSString *name = @"Chelsea";
NSNumber *points = @56; // or whatever your objects are

NSDictionary *extraTeamInfo = @{@"teamFullName" : @"Chelsea F.C.",
                                @"teamStadiumName" : @"Stamford Bridge",
                                @"stadiumCapacity" : @41837,
                                @"clubFoundationDate" : @1905}; // etc. fill all fields

// repeat this for each team and add each dictionary into the array however you obtain the data
// you may think a better array/dictionary structure for easier updates based on your use case

[self.teamArray addObject:@[teamID, name, points, extraTeamInfo]];

然后你的cellForRowAtIndexPath可能如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.nameLabel.text    cell.nameLabel.text = [self.teamArray[indexPath.row] objectAtIndex:1];
    NSUInteger points = [[[self.teamArray[indexPath.row] objectAtIndex:2]; integerValue];
    cell.pointsLabel.text = [NSString stringWithFormat:@"%d", points];

    return cell;
}

并准备观看:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"teamDetailsSeg"]) {

        TeamDetailsTableViewController *detailViewController = [segue destinationViewController];
        UITableViewCell *cell = (UITableViewCell *)sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        detailViewController.extraTeamInfo = [self.teamArray[indexPath.row] lastObject];
    }
}