Reputation: 6357
I have an odd error occurring when populating a table view. This is something I've done countless times but now I'm stumped over something that is probably pretty simple. Maybe I've just been awake too long.
I have an object called riverGauge that is responsible for downloading data from a web service. The relevant data is then placed into an NSDictionary object and returned as a property. An array of keys built from the keys in the dictionary is also returned as a property.
In my cellForRowAtIndexPath method, I am using the array of keys to look up values in the dictionary and render them in the table view. Pretty standard. Here is my cellForRowAtIndexPath:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"DataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
NSDate *key = [riverGauge.gaugeFlowKeys objectAtIndex:indexPath.row];
NSString *cellText = [riverGauge.gaugeFlows objectForKey:key];
NSLog(@"CELL TEXT = %@", cellText);
[cell.textLabel setText:cellText]; // Error on last pass. No errors without this line
return cell;
}
After the last cell is populated I am getting an unrecognized selector exception.
2014-08-28 12:07:20.318 RiverGuage[9858:60b] -[__NSCFNumber length]: unrecognized selector sent to instance 0xa07b180
2014-08-28 12:07:20.319 RiverGuage[9858:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xa07b180'
I have verified that the number of keys returned matches the number of records in the dictionary and that there are values for the keys specified. The log statement works for every item in the list and doesn't return anything out of the ordinary. The cellForRowAtIndexPath method returns without error if the [cell.textLabel setText:cellText] is commented. Any ideas on why this is happening? THanks!
Upvotes: 0
Views: 157
Reputation: 5149
My best guess is that your [riverGauge.gaugeFlowKeys objectAtIndex:indexPath.row]
is returning a NSNumber
. In that case do the following to set the number anyway.
NSNumber *cellTextNumber = [riverGauge.gaugeFlows objectForKey:key];
NSString *cellText = [cellTextNumber stringValue];
NSLog(@"CELL TEXT = %@", cellText);
[cell.textLabel setText:cellText];
Upvotes: 1