我有一个对象数组_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;
}
答案 0 :(得分: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;
}