0

我有一个对象数组,_schedule.games我想在每个游戏中显示游戏属性对手,因为我循环通过时间表。

    int x = 0;
    for (int i = 0; i < [_schedule.games count]; i++)
    {
        Game *game = [_schedule.games objectAtIndex:i];
        game.opponent = ((Game *) [_schedule.games objectAtIndex:i]).opponent;
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];

        [button setTitle:[NSString stringWithFormat:@"%@", game.opponent] forState:UIControlStateNormal];

        [_gameScrollList addSubview:button];

        x += button.frame.size.width;

    }
4

1 回答 1

1

1.

    Game *game = [_schedule.games objectAtIndex:i];

为您提供数组内的游戏实例,因此无需再次分配属性

game.opponent = ((Game *) [_schedule.games objectAtIndex:i]).opponent;

game.opponent具有数组对象属性中的值,因此您可以直接将其称为game.opponent.

2.

[NSString stringWithFormat:@"%@", game.opponent]game.opponent是一个字符串,因此无需再次将其类型转换为NSString

所以方法将是

int x = 0;
for (int i = 0; i < [_schedule.games count]; i++)
{
    Game *game = (Game *)[_schedule.games objectAtIndex:i];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
    [button setTitle:game.opponent forState:UIControlStateNormal];
    [_gameScrollList addSubview:button];
    x += button.frame.size.width;
}
于 2013-08-19T05:50:14.623 回答