JoeShmoe
JoeShmoe

Reputation: 37

Failed to obtain a cell from its dataSource when loading UIView with Table

I have a ViewController in which I put another small UIView in the middle where I have placed a TableView but not sure how to display data from an array in this table, any help would be greatly appreciated. I'm trying to load the table using this code:

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

}];

}

But now I'm receiving a Failed to obtain a cell from its dataSource. Heres the code for the table

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

and heres the .h for this view controller

#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

I get this error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell resultsIcon]: unrecognized selector sent to instance" I'm stuck not sure how to proceed. Thank You in advance for any help or guidance you could provide me.

oh yeah heres the resultsViewCell.h

@interface resultsViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UIImageView *resultsFlag;
@property (strong, nonatomic) IBOutlet UILabel *resultsName;
@property (strong, nonatomic) IBOutlet UIImageView *resultsIcon;

@end

Upvotes: 1

Views: 3702

Answers (2)

Ketan Parmar
Ketan Parmar

Reputation: 27438

First check your questionArray. It must not have any empty object.

Second if you are using cell from storyboard means direct from tableview, then why are you registering it?

Upvotes: 0

Glenn Posadas
Glenn Posadas

Reputation: 13281

Based on the codes above you provided, I assume you are using a custom cell. You should register that resultsViewCell

Register that custom cell in viewDidLoad

[self.resultsTable registerNib:[UINib nibWithNibName:@"NAME_OF_YOUR_CELL_NIB" bundle:nil] forCellReuseIdentifier:@"resultsListCell"];

And I noticed that inside your animateResults method, you are assigning self to a delegate and datasource of resultsTable, shouldn't it be self.resultsTable or _resultsTable?

Upvotes: 1

Related Questions