2016-04-12 108 views
0

我試圖用通過修改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; 
} 

但是,此代碼沒有任何顯示。我仍然在學習我已經設法達到這一點。我已經能夠正確地運行十個問題,但現在我正在努力完成這個最後的部分,說實話,我被困在了愚蠢的地方。只是一直沒能弄清楚。任何幫助將不勝感激。

+0

你應該叫[resultsTable reloadData]更新questionsArray後。 – Pankaj

回答

0

如果您修改NSMutableArray,請在成功修改後調用[tableView reloadData]。希望這會有所幫助。 :)

0

您需要將表視圖的委託和數據源設置到您的視圖控制器,然後如果數據已準備就緒,請調用表視圖的reloadData方法。

嘗試查看文檔,瞭解詳情: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10

+0

我試圖通過控制拖動到GamePlayViewController來添加我的表視圖的委託和數據源。並將[resultsTable reloadData]添加到我的answerMethod的末尾。但是收到一個數據源失敗(錯誤),一旦遊戲嘗試加載子視圖似乎試圖在數組創建之前加載表,謝謝您的回答和參考資料。我將在繼續之前徹底地給出它。 – JoeShmoe

+0

您需要註冊您的單元類' - (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier',否則,'static NSString * CellIdentifier = @「resultsListCell」 ; resultsViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];'將得到零 –

+0

感謝您的所有幫助到目前爲止,我試圖加載這個UIView _resultsView與最後的分數和球員的問題結果。但仍然無濟於事我試圖添加我的表視圖的委託和數據源通過控制拖動到GamePlayViewControlle河並將[resultsTable reloadData]添加到我的answerMethod的末尾。但收到一個未能從其dataSource獲取單元格(錯誤,並且還試圖註冊我的類仍然在加載GamePlayViewController時崩潰。我知道它的用戶錯誤。 – JoeShmoe

相關問題