2014-02-10 36 views
0

我有一個包含對象的數組,這些對象顯示在TableViewController中,每行都有一些字段,如team id,namepoints如何根據值在prepareforSegue中顯示數組中的條目?

現在,我創建了一個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 

    } 
} 
+0

爲了篩選的目的,你可以使用字典而不是數組結構:@(team_id)=> ExtraTeamInfoObject – vokilam

回答

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

而且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]; 
    } 
} 
+0

我試過你的解決方案,但它似乎沒有工作,當我點擊一排它需要我到其他賽格,但是segue標籤是空的:http://pastebin.com/qf5njjc7 – user3271300

+0

嗯,我沒有完整的代碼,所以解決方案只是一個提示,你應該去哪個方向。我不知道teamID類型等等。如果你提供了與這個主類以及細節相關的完整代碼,即使在數組中有一些僞數據,我也可以查看它。你應該真的檢查NSDictionary。查看我的編輯,瞭解如何處理它。 –

相關問題