Reputation: 55
I'm using the below code to use two different custom table cells in my table view. Everything looks right, however when I run the app, I get the following error:
Failed to obtain a cell from its dataSource FullMessageViewController: 0x103d61c90
Any idea why this might be?
FullMessageViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
static NSString *ChatTableIdentifier = @"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";
UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];
UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
[self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ChatTableIdentifier = @"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";
NSDictionary *data = [self.messages objectAtIndex:indexPath.row];
if (![data objectForKey:@"field_swaptime"]) {
NSLog(@"THIS IS DATA %@", data);
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];
NSString *userName = [data objectForKey:@"name"];
[cell.sendingUser setText:userName];
NSString *messageBody = [data objectForKey:@"body"];
[cell.messageDisplayed setText:messageBody];
NSString *timeReceived = [data objectForKey:@"published at"];
NSLog(@"Message Received at %@", timeReceived);
[cell.timeStamp setText:timeReceived];
return cell;
}
else {
SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];
NSString *Time = [data objectForKey:@"field_swaptime"];
NSLog(@"This is time %@", Time);
[cell.startTime setText:Time];
NSString *TimeEnd = [data objectForKey:@"field_endswaptime"];
[cell.endTime setText:TimeEnd];
return cell;
}
}
Upvotes: 0
Views: 78
Reputation: 318934
Since you are registering a nib for your cell types, you need to replace the use of dequeueReusableCellWithIdentifier:
with dequeueReusableCellWithIdentifier:forIndexPath:
.
Example. Change:
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier];
to:
ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];
And you have some typos in viewDidLoad
. Update it as follows:
- (void)viewDidLoad {
[super viewDidLoad];
static NSString *ChatTableIdentifier = @"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";
UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];
UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
[self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];
}
Upvotes: 1
Reputation: 149
Check this line on viewDidLoad
[self.tableView registerNib:nib2 forCellReuseIdentifier:@"ChatTableIdentifier2"];
You're registering a nib with the identifier ChatTableIdentifier2
but you don't actually use that identifier in cellForRowAtIndexPath, you use SwapDetailTableViewCell
.
Changing that line to
[self.tableView registerNib:nib2 forCellReuseIdentifier:@"SwapDetailTableViewCell"];
should work
Upvotes: 1