[英]TableView data pass into another tableview with same UIVIewController?
[英]How to pass NSMutableArray data from a UIViewController to a TableView subview
我試圖用通過修改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
子視圖是此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
游戲循環在此View Controller 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;
}
但是這個代碼揭示了什么。 我仍在學習,我已經設法做到這一點。 我已經能夠解決10個問題,但現在我正嘗試完成最后一部分,說實話,我陷入了愚蠢的困境。 只是無法弄清楚。 任何幫助將不勝感激。
在您修改NSMutableArray
,成功修改后調用[tableView reloadData]
。 希望這會有所幫助。 :)
你需要你的表視圖的委托和數據源設置為您的視圖控制器,那么如果數據已准備好,調用表視圖的reloadData方法。
嘗試查看文檔以了解更多信息: https : //developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html#//apple_ref/doc/uid/TP40007451-CH6-SW10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.