【问题标题】:How to pass NSMutableArray data from a UIViewController to a TableView subview如何将 NSMutableArray 数据从 UIViewController 传递到 TableView 子视图
【发布时间】:2016-04-12 04:48:22
【问题描述】:

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

}

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

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

但是这段代码什么也没显示。我仍在学习我已经设法达到这一点。我已经能够让这十个问题正常运行,但现在我正试图完成最后一部分,老实说,我陷入了愚蠢的境地。只是一直无法弄清楚。任何帮助将不胜感激。

【问题讨论】:

  • 你应该在更新你的 questionsArray 之后调用 [resultsTable reloadData]。

标签: ios objective-c uitableview nsmutablearray


【解决方案1】:

修改NSMutableArray的地方,修改成功后拨打[tableView reloadData]。希望这会有所帮助。 :)

【讨论】:

    【解决方案2】:

    你需要将你的table view的delegate和data source设置到你的view controller,然后如果数据准备好了,调用table view的reloadData方法。

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

    【讨论】:

    • 我尝试通过将控件拖动到 GamePlayViewController 来添加我的表格视图的委托和数据源。并将 [resultsTable reloadData] 添加到我的 answerMethod 的末尾。但是收到一个 failed to get a cell from its dataSource ( 错误。一旦游戏尝试加载子视图似乎是在创建数组之前尝试加载表。感谢您的回答和参考资料。在继续之前,我会彻底阅读。
    • 你需要注册你的单元格- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier ,否则static NSString *CellIdentifier = @"resultsListCell"; resultsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];将得到nil。
    • 感谢您迄今为止的所有帮助,我正在尝试加载此 UIView _resultsView 以及最终得分和玩家提问结果。我尝试了您的建议,但仍然无济于事。我尝试通过将控件拖动到 GamePlayViewController 来添加我的表视图的委托和数据源。并将 [resultsTable reloadData] 添加到我的 answerMethod 的末尾。但是收到了一个无法从其数据源获取单元格( 错误。并且还尝试注册我的类在加载 GamePlayViewController 时仍然崩溃。我知道它的用户错误。
    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多