2014-08-28 26 views
0

填充表视图时出现奇怪的错误。这是我做过无数次的事情,但现在我很难理解那些可能非常简单的事情。也许我刚睡了太久。错误cellForRowAtIndexPath

我有一个名为riverGauge的对象,负责从Web服务下载数据。然后将相关数据放置到NSDictionary对象中并作为属性返回。从字典中的键构建的键的数组也作为属性返回。

在我的cellForRowAtIndexPath方法中,我使用键数组来查找字典中的值并将它们呈现在表视图中。相当标准。这是我的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; 
} 

最后一个单元格填充我正在一个无法识别的选择异常后。

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' 

我已验证返回的键数与字典中的记录数匹配,并且指定的键有值。日志语句适用于列表中的每个项目,并且不会返回任何不寻常的内容。如果注释了[cell.textLabel setText:cellText],cellForRowAtIndexPath方法将无错地返回。任何想法为什么发生这种情况?谢谢!

+0

你最近一次通过的日志('cellText')是什么? – Larme 2014-08-28 16:25:37

+0

您正在尝试将NSNumber对象设置为单元格文本 – Injectios 2014-08-28 16:26:36

+0

这是字典中的最后一项 - 正是我期望的 – Pheepster 2014-08-28 16:27:03

回答

1

我最好的猜测是你的[riverGauge.gaugeFlowKeys objectAtIndex:indexPath.row]返回NSNumber。在这种情况下,请按照以下步骤设置号码。

NSNumber *cellTextNumber = [riverGauge.gaugeFlows objectForKey:key]; 
NSString *cellText = [cellTextNumber stringValue]; 
NSLog(@"CELL TEXT = %@", cellText); 
[cell.textLabel setText:cellText]; 
相关问题