Ios 如何将NSMutableArray数据从UIViewController传递到TableView子视图
我试图用通过修改Ios 如何将NSMutableArray数据从UIViewController传递到TableView子视图,ios,objective-c,uitableview,nsmutablearray,Ios,Objective C,Uitableview,Nsmutablearray,我试图用通过修改NSMutableArray创建的数据填充子视图表。基本上,一个人回答10个问题,我想显示一个包含正确答案图像的子视图,并标记一个复选标记图像,以确定用户的问题是对的还是错的。我在tableview中创建了一个自定义单元格,并将它们链接到一个表格单元格视图控制器(resultCellView) 游戏循环在此视图控制器GamePlayViewController.m文件上运行,然后调用函数animate results: - (void)gameLoop { if (loopCo
NSMutableArray
创建的数据填充子视图表。基本上,一个人回答10个问题,我想显示一个包含正确答案图像的子视图,并标记一个复选标记图像,以确定用户的问题是对的还是错的。我在tableview中创建了一个自定义单元格,并将它们链接到一个表格单元格视图控制器(resultCellView)
游戏循环在此视图控制器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) 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];
}
修改后的问题是创建的,但正如我所说,我不确定如何从这一点出发。我已尝试将此代码添加到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;
}
但这段代码什么也没透露。我仍在学习,我已经设法做到了这一点。我已经能够让这十个问题正常运行,但现在我正在努力完成这最后一部分,老实说,我陷入了愚蠢。只是没能弄明白而已。非常感谢您的帮助。如果您修改了
NSMutableArray
,请在成功修改后调用[tableView reloadData]
。希望这会有所帮助。:) 您需要将表视图的委托和数据源设置为视图控制器,然后如果数据已准备好,则调用表视图的重载数据方法
尝试查看文档以了解更多信息:
更新问题后,您应该调用[resultsTable reloadData]Ray。我试图通过将控件拖动到GamePlayViewController来添加我的表视图的委托和数据源。并将[resultsTable reloadData]添加到我的answerMethod的末尾。但是收到一个未能从其数据源获取单元格的消息(错误。一旦游戏尝试加载,子视图似乎正在尝试在创建数组之前加载表。感谢您的回答和参考资料。在继续之前,我将彻底阅读它。您需要注册您的单元格类
-(void)registerClass:(nullable class)cellClass forCellReuseIdentifier:(NSString*)标识符
,否则,静态NSString*CellIdentifier=@“resultsListCell”;resultsViewCell*cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]
将为零。感谢您迄今为止的所有帮助,我正在尝试加载此UIViewresultsView,其中包含最终分数和玩家提问结果。我尝试了您的建议,但仍然无效。我尝试通过将控件拖动到GamePlayViewController来添加表视图的委托和数据源。并添加了[resultsTable reloadData]到我的answerMethod的结尾。但是收到一个未能从其数据源获取单元格的消息(错误)。并且尝试注册我的类在加载GamePlayViewController时仍然崩溃。我知道它的用户错误。
-(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);
}];
}
- (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];
}
-(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;
}