我有一个ViewController,我在其中放置了另一个小UIView,我放置了一个TableView,但不知道如何在此表中显示数组中的数据,我们将非常感谢任何帮助。我尝试使用以下代码加载表格:
-(void) animateResults {
_resultsLabel.text = [NSString stringWithFormat:@"You Scored %d", runningScore ];
resultsTable.delegate = self;
resultsTable.dataSource = self;
[self.resultsTable registerClass:[UITableViewCell self] forCellReuseIdentifier:@"resultsListCell"];
[self.resultsTable reloadData];
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
_resultsView.frame = self.view.frame;
} completion:^(BOOL finished){
NSLog(@"%@", questionsArray);
}];
}
但是现在我收到了一个无法从其dataSource获取一个单元格。下面是表格的代码
-(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;
}
并且继承了这个视图控制器的.h
#import <UIKit/UIKit.h>
#import "Store.h"
#import "flags.h"
#import "Tribes.h"
#import "resultsViewCell.h"
@interface GamePlayViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>{
NSMutableArray *questionsArray;
NSMutableArray *answersArray;
NSInteger gameSelected;
NSMutableArray *revealArray;
NSTimer *questionTimer;
BOOL GameInProgress;
int random;
int questionTimerCounter;
int loopCounter;
int runningScore;
}
@property (weak, nonatomic) IBOutlet UIImageView *questionImage;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel1;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel2;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel3;
- (IBAction)answer1:(id)sender;
- (IBAction)answer2:(id)sender;
- (IBAction)answer3:(id)sender;
@property (weak, nonatomic) IBOutlet UIImageView *cover1;
@property (weak, nonatomic) IBOutlet UIImageView *cover2;
@property (weak, nonatomic) IBOutlet UIImageView *cover3;
@property (weak, nonatomic) IBOutlet UIImageView *cover4;
@property (weak, nonatomic) IBOutlet UIImageView *cover5;
@property (weak, nonatomic) IBOutlet UIImageView *cover6;
@property (weak, nonatomic) IBOutlet UIImageView *cover7;
@property (weak, nonatomic) IBOutlet UIImageView *cover8;
@property (weak, nonatomic) IBOutlet UIView *coreView;
@property (weak, nonatomic) IBOutlet UILabel *resultsLabel;
@property (strong, nonatomic) IBOutlet UITableView *resultsTable;
@property (weak, nonatomic) IBOutlet UIView *resultsView;
@end
我收到此错误&#34;由于未捕获的异常终止应用&#39; NSInvalidArgumentException&#39;,原因:&#39; - [UITableViewCell resultsIcon]:无法识别的选择器发送到实例&#34; 我不知道该怎么办。提前感谢您提供给我的任何帮助或指导。
哦,是的,继承人的结果ViewCell.h
@interface resultsViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *resultsFlag;
@property (strong, nonatomic) IBOutlet UILabel *resultsName;
@property (strong, nonatomic) IBOutlet UIImageView *resultsIcon;
@end
答案 0 :(得分:1)
根据您提供的上述代码,我假设您使用的是自定义单元格。您应该注册 resultsViewCell
在 viewDidLoad
中注册该自定义单元格[self.resultsTable registerNib:[UINib nibWithNibName:@"NAME_OF_YOUR_CELL_NIB" bundle:nil] forCellReuseIdentifier:@"resultsListCell"];
我注意到在 animateResults 方法中,您将自己分配给 resultsTable 的委托和数据源,不应该是 self。 resultsTable 或 _resultsTable ?
答案 1 :(得分:0)
首先检查您的questionArray
。它不能有任何空对象。
第二,如果您使用故事板中的单元格意味着直接来自tableview
,那么您为什么要注册它?