简体   繁体   中英

How to display an entry from an array in a prepareforSegue according to a value?

I have one array that has objects, and these objects are displayed in a TableViewController and each row has some fields, like team id , name and points .

Now I created an additional team info array with more objects/properties as - full team name, foundation year and team id again etc.

What I want is that, when the user clicks in a row in my TableViewController it reads the team id and in the prepareforSegue has some way of searching my additional team info array and find the matching team id and display that object data in another segue. Below is some of my code:

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

    }
}

I would do something like this:

- (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;
            }
        }
    }
}

But I agree with vokilam. NSDictionary is much better choice for you.

Edit

Something like:

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

And then your cellForRowAtIndexPath might look like:

- (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;
}

And prepareForSegue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM