我試圖用通過修改NSMutableArray
創建的數據填充子視圖表。基本上,一個人回答10個問題,我想用正確的答案圖像顯示子視圖,並標記對號圖像以確定用戶是否正確或不正確。我在tableview中創建了一個自定義單元格,並將它們鏈接到一個表格單元格視圖控制器(resultCellView)。如何將NSMutableArray數據從UIViewController傳遞到TableView子視圖
#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
遊戲循環在這個視圖控制器GamePlayViewController.m
文件運行的一部分,然後調用該函數動畫效果:
- (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;
}
但是,此代碼沒有任何顯示。我仍然在學習我已經設法達到這一點。我已經能夠正確地運行十個問題,但現在我正在努力完成這個最後的部分,說實話,我被困在了愚蠢的地方。只是一直沒能弄清楚。任何幫助將不勝感激。
你應該叫[resultsTable reloadData]更新questionsArray後。 – Pankaj