将徽标添加到数据结构中

时间:2014-02-05 01:22:15

标签: ios objective-c xml uiimage

我有一个tableviewcell,它有一个团队徽标的UIImage,我从第三方XML Feed获取我的数据,作为团队名称,分数等,它没有包含徽标数据。

所以我需要将这些信息添加到该数据中。

问题是我不知道该怎么做。

我的parseXML方法有效,一切正常,但我的uiimage for logo正由一个包含所有.png文件的硬编码数组填充,唯一的事情就是如果number1中的团队下降到number2,徽标不会更新。

请参阅下面的代码,我在解析后已经包含了我的logos数组和xml数据结构。

任何人都可以提供我如何解决问题的示例代码吗?我已经被困在这里一段时间了。

-(void) parseXML{

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"apikeygoeshere"]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];

    NSMutableArray *items = [xml objectForKey:@"TeamLeagueStanding"];

    NSString *nullentry = @""; // custom code for specific reason
    NSString *nullentry2 = @""; // custom code for a specific reason

    [items insertObject:nullentry atIndex:0]; // custom code for a specific reason
    [items insertObject:nullentry2 atIndex:1]; // custom code for a specific reason

    [self setTableData:items];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"StandingsIdent";
    StandingsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

     NSDictionary *item = [tableData objectAtIndex:[indexPath row]];

    if ([item isKindOfClass:[NSDictionary class]]) {

    long row = [indexPath row];

    cell.cellTeamName.text = [item objectForKey:@"Team"];;
    cell.cellTeamLogo.image = [UIImage imageNamed:_teamLogos[row]];
    cell.cellTeamPosition.text = _teamPosition[row];
    cell.cellPlayed.text = [item objectForKey:@"Played"];
    cell.cellWins.text = [item objectForKey:@"Won"]; 
    cell.cellTies.text = [item objectForKey:@"Draw"]; 
    cell.cellLoses.text = [item objectForKey:@"Lost"]; ;
    cell.cellPoints.text = [item objectForKey:@"Points"]; 
    cell.cellInfo.text = _infoLeague[row];

    }

 else {

    }
 }

 LOGO ARRAY

     _teamLogos = @[@"",
                   @"",
                   @"1478.png",
                   @"1487.png",
                   @"1489.png",
                   @"1494.png",
                   @"1474.png",
                   @"2390.png",
                   @"2433.png",
                   @"1488.png",
                   @"1481.png",
                   @"2383.png",
                   @"1476.png",
                   @"1495.png",
                   @"729500.png",
                   @"2386.png",
                   @"2445.png",
                   @"2393.png",
                   @""];


XML DATA STRUCTURE AFTER BEING PARSED

Draw = 10;
"Goal_Difference" = "-17";
"Goals_Against" = 39;
"Goals_For" = 22;
Lost = 11;
NumberOfShots = 395;
Played = 25;
PlayedAtHome = 13;
PlayedAway = 12;
Points = 22;
RedCards = 5;
Team = Partick;
"Team_Id" = 561;
Won = 4;
YellowCards = 41;
}

1 个答案:

答案 0 :(得分:0)

不要将硬编码的图像名称放在数组中,只需将图像命名为与团队名称相同或更好的团队ID。

然后你可以这样做:

cell.cellTeamLogo.image = [UIImage imageNamed:item[@"Team_Id"]];

这使事情变得简单明了。如果您添加新的团队,只需添加具有相同团队ID的新图片(添加了.png扩展名)。