如何将NSMutableArray数据从UIViewController传递到TableView子视图

时间:2016-04-12 04:48:23

标签: ios objective-c uitableview nsmutablearray

我正在尝试使用修改NSMutableArray创建的数据填充子视图表。基本上,一个人回答10个问题,我想用正确的答案图像显示子视图,并标记一个复选标记图像,以确定用户是否正确或错误。我在tableview中创建了一个自定义单元格,并将它们链接到表格单元格视图控制器(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];
}

}

animate结果函数会填充最终得分,但我不确定如何继续填充表格。

-(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;
}

但这段代码没有透露任何内容。我还在学习,我已经设法达到这一点。我已经能够正确地运行十个问题了,但现在我试图完成这个最后一部分,说实话,我已经陷入了愚蠢的境地。只是还没弄明白。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

修改NSMutableArray的位置,请在成功修改后致电[tableView reloadData]。希望这会有所帮助。 :)

答案 1 :(得分:0)

您需要将表视图的委托和数据源设置为视图控制器,然后如果数据准备就绪,请调用表视图的reloadData方法。

尝试查看文档以了解更多信息: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10