【发布时间】:2016-04-12 04:48:22
【问题描述】:
我正在尝试使用通过修改 NSMutableArray 创建的数据填充子视图表。基本上,一个人回答了 10 个问题,我想显示一个带有正确答案图像的子视图,并标记一个复选标记图像,以确定用户的问题是对还是错。我在表格视图中创建了一个自定义单元格并将它们链接到表格单元格视图控制器(resultCellView)。
#import <UIKit/UIKit.h>
@interface resultsViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *resultsFlag;
@property (weak, nonatomic) IBOutlet UILabel *resultsName;
@property (weak, nonatomic) IBOutlet UIImageView *resultsIcon;
@end
子视图是这个视图控制器GamePlayViewController.h的一部分
#import <UIKit/UIKit.h>
#import "resultsViewCell.h"
@interface GamePlayViewController : UIViewController {
NSMutableArray *questionsArray;
NSMutableArray *answersArray;
NSInteger gameSelected;
NSMutableArray *revealArray;
NSTimer *questionTimer;
BOOL GameInProgress;
int random;
int questionTimerCounter;
int loopCounter;
int runningScore;
}
@property (weak, nonatomic) IBOutlet UILabel *resultsLabel;
@property (weak, nonatomic) IBOutlet UITableView *resultsTable;
@property (weak, nonatomic) IBOutlet UIView *resultsView;
@end
游戏循环在这个 View Controller GamePlayViewController.m 文件上运行,然后调用函数 animate results:
- (void)gameLoop {
if (loopCounter < 10){
revealArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", nil];
cover1.alpha = 1.0;
cover2.alpha = 1.0;
cover3.alpha = 1.0;
cover4.alpha = 1.0;
cover5.alpha = 1.0;
cover6.alpha = 1.0;
cover7.alpha = 1.0;
cover8.alpha = 1.0;
coreView.alpha = 1.0;
NSDictionary *question = [[NSDictionary alloc]init];
question = [questionsArray objectAtIndex:loopCounter];
questionImage.image = [UIImage imageNamed:[question objectForKey:@"imageNames"]];
[self getAnswers];
}
else {
//NSLog(@"finished");
[self animateResults];
}
}
动画结果函数会填充最终分数,但我不确定如何继续填充表格。
-(void) animateResults {
_resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore ];
[UIView animateWithDuration:0.3 animations:^{
_resultsView.frame = self.view.frame;
} completion:^(BOOL finished){
NSLog(@"%@", questionsArray);
}];
}
我 NSLog 用这个方法创建的 questionsArray:
- (void) answerMethod:(int)answerBtn {
if (answerBtn == random) {
//NSLog(@"correct!");
int maxScore = 10000;
int userScore = maxScore - (questionTimerCounter*1000);
NSLog(@"%d", userScore);
runningScore = runningScore + userScore;
NSLog(@"%d" , runningScore);
NSMutableDictionary *question = [[NSMutableDictionary alloc]init];
question = [[questionsArray objectAtIndex:loopCounter]mutableCopy];
[question setObject:[NSNumber numberWithBool:YES] forKey:@"correct"];
[question setObject:[NSNumber numberWithInt:userScore] forKey:@"score"];
[questionsArray replaceObjectAtIndex:loopCounter withObject:question];
}
else {
// NSLog(@"incorrect");
NSMutableDictionary *question = [[NSMutableDictionary alloc]init];
question = [[questionsArray objectAtIndex:loopCounter]mutableCopy];
[question setObject:[NSNumber numberWithBool:NO] forKey:@"correct"];
[question setObject:[NSNumber numberWithInt:0] forKey:@"score"];
[questionsArray replaceObjectAtIndex:loopCounter withObject:question];
}
GameInProgress = NO;
loopCounter++;
[self revealAnswer];
}
修改后的 questionsArray 已创建,但就像我说的那样,我不确定如何从这一点开始。我尝试将此代码添加到GamePlayViewController.m 文件:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//Return number of sections
return 1;
}
//get number of rows by counting number of challenges
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return questionsArray.count;
}
//setup cells in tableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//setup cell
static NSString *CellIdentifier = @"resultsListCell";
resultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *results = [questionsArray objectAtIndex:indexPath.row];
NSString *resultsName = [results objectForKey:@"answers"];
BOOL correct = [[results objectForKey:@"correct"] boolValue];
if (!correct) {
cell.resultsIcon.image = [UIImage imageNamed:@"BlackIconLock.png"];
}
else{
cell.resultsIcon.image = nil;
}
cell.resultsName.text = resultsName;
return cell;
}
但是这段代码什么也没显示。我仍在学习我已经设法达到这一点。我已经能够让这十个问题正常运行,但现在我正试图完成最后一部分,老实说,我陷入了愚蠢的境地。只是一直无法弄清楚。任何帮助将不胜感激。
【问题讨论】:
-
你应该在更新你的 questionsArray 之后调用 [resultsTable reloadData]。
标签: ios objective-c uitableview nsmutablearray