简体   繁体   中英

How to pass NSMutableArray data from a UIViewController to a TableView subview

I am trying to populate a subview table with data created by modifying an NSMutableArray . Basically, a person answers 10 questions and I want to show a subview with the correct answer image and label also a checkmark image to determine if the user got the question right or wrong. I have created a custom cell inside the tableview and linked them to a table cell view controller (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

The subview is part of the this View Controller 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

The game loop runs on this View Controller GamePlayViewController.m File and then calls the function 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];
}

}

the animate results function populates the final score but I am unsure how to proceed populating table.

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

}];


}

I NSLog the questionsArray created by this method:

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

}

The modified questionsArray is created but like I said I am unsure how to proceed from this point. I've tried adding this code to GamePlayViewController.m file:

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

But this code reveals nothing. I am still learning I've managed to get to this point. I've been able to get the ten questions to run properly but now I'm trying to get this final part done and to be honest I'm stuck on stupid. Just Have not been able to get it figured out. any HELP would be greatly appreciated.

Where you modifying your NSMutableArray , call [tableView reloadData] after successfully modification. Hope this will help. :)

You need set the delegate and data source of your table view to your view controller, then if the data is ready, call reloadData method of table view.

try to view the doc to learn more: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM