我正在构建一个测验应用程序,我从API中获取问题。我有2个视图(称为frontView和backView),每个视图都有1个图像(称为frontImageView和backImageView)和4个按钮(但是现在我只为每个视图添加了1个按钮作为frontAnswer1和backAnswer1)。 我正在尝试根据_results数组中的项目数一个接一个地更改这些视图
当进行调用以获取JSON项时,这是我得到的一个例子:
AccountId = 0;
Answers = (
"a",
"b",
c,
"d"
);
CategoryId = 2;
CategoryName = "abc";
CorrectAnswer = "b";
Difficulty = 1;
Id = 24;
PicUrl = "http://someURL";
然后我循环通过这个json来获得答案和图片网址:
for (NSDictionary * itemDict in array) {
Item *myResponseItems = [[Item alloc] initWithDictionary:itemDict];
[tempArray addObject:myResponseItems];
}
_results = tempArray;
然后我将对象分配到我的视图中的适当位置,如下所示:
// _ JSONObjectsForTheFrontView和_JSONObjectForTheBackView是我的item类的类实例,我在其中解析json对象。 // _ results数组包含我想要显示的所有项目。
_JSONObjectsForTheFrontView = [_results objectAtIndex:0];
[_frontAnswerButton1 setTitle:[_JSONObjectsForTheFrontView.Answers objectAtIndex:0] forState:UIControlStateNormal];
_frontImageView.image = _JSONObjectsForTheFrontView.image;
_JSONObjectForTheBackView =[_results objectAtIndex:1];
[_backAnswer1 setTitle:[_JSONObjectForTheBackView.Answers objectAtIndex:1] forState:UIControlStateNormal];
_backImageView.image =_JSONObjectForTheBackView.image;
所以我正在尝试创建一个结构,我可以根据我的数组计数显示视图。 这是我到目前为止: GameView.h
@property (nonatomic,retain) Item *JSONObjectsForTheFrontView;
@property (nonatomic,retain) Item *JSONObjectForTheBackView;
@property (nonatomic,assign)NSInteger currentIndexInTheResultsArray;
@property (nonatomic, assign)NSInteger viewCount;
@property (nonatomic,strong)NSIndexPath *CurrentIndexPathForTheResutsArray;
GameView.m // _ currentIndexInTheResultsArray是一个整数,我保存了我有多少项。 // _ viewCount只是一个设置为0的整数。 _currentIndexInTheResultsArray = [_results count]; NSLog(@“当前索引中有%ld项目”,(长)_currentIndexInTheResultsArray);
for (_viewCount = 0; _viewCount<_currentIndexInTheResultsArray;_viewCount++ ) {
if (_currentIndexInTheResultsArray %2 ==0) {
_JSONObjectsForTheFrontView = [_results objectAtIndex:_viewCount];
[_frontAnswer1 setTitle:[_JSONObjectsForTheFrontView.Answers objectAtIndex:0] forState:UIControlStateNormal];
_frontImageView.image = _JSONObjectsForTheFrontView.image;
}
else if (_currentIndexInTheResultsArray %2 ==1)
{
_JSONObjectForTheBackView =[_results objectAtIndex:_viewCount];
[_backAnswer1 setTitle:[_JSONObjectForTheBackView.Answers objectAtIndex:1] forState:UIControlStateNormal];
_backImageView.image =_JSONObjectForTheBackView.image;
}
}
当我跑的时候。我在第一页中看到一个空视图,在第二页中看到最后一个问题。 如果有人可以帮助我,那就太好了。谢谢。