【问题标题】:Unable to display dictionary object value in label [duplicate]无法在标签中显示字典对象值 [重复]
【发布时间】:2015-03-19 20:39:51
【问题描述】:

我在表格中显示了一组来自 Web 服务的字典。这是 cellForRowAtIndexPath 中的代码 sn-p。

cell.businessNameLabel.text = [dataDict objectForKey:@"business_name"];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:@"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];

cell.serviceTypeLabel.text = [dataDict objectForKey:@"address"];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:@"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];

cell.businessID = [dataDict objectForKey:@"business_id"];
cell.reviewScoreLabel.text = [dataDict objectForKey:@"rating"];

return cell;

应用程序在以下行崩溃:

cell.reviewScoreLabel.text = [dataDict objectForKey:@"rating"];

具有以下异常详细信息:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance 0xb000000000000003'

字典对象数据如下所示:

address = "Boston, MA, United States";
"business_id" = 15;
"business_image" = "http://demo.web.com/home-business/upload/user/1420452418_.jpeg";
"business_name" = autodealersma;
lat = "42.3584865";
lng = "-71.06009699999998";
rating = "4.3";    
service = "auto dealers";

【问题讨论】:

  • “4.3”似乎是NSNumber 而不是NSString
  • 我认为这是因为您试图将 NSNumber 设置为文本属性。 text 属性需要一个 NSString。你可以试试:cell.reviewScoreLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"rating"]]; 请看下面我的回答。
  • 非常感谢门诺。它的工作原理!

标签: ios objective-c


【解决方案1】:

试试这个...

cell.businessNameLabel.text = [[dataDict objectForKey:@"business_name"]objectAtIndex:indexPath.row];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:@"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];

cell.serviceTypeLabel.text = [[dataDict objectForKey:@"address"]objectAtIndex:indexPath.row];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:@"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];

cell.businessID = [[dataDict objectForKey:@"business_id"]objectAtIndex:indexPath.row];
cell.reviewScoreLabel.text = [[dataDict objectForKey:@"rating"]objectAtIndex:indexPath.row];

return cell;

【讨论】:

  • 现在检查我已经编辑了答案
  • 现在它崩溃了。我认为类型转换是解决方案
【解决方案2】:

修改你的代码为

cell.businessNameLabel.text = [NSString stringWithFormat:@"%@", [dataDict objectForKey:@"business_name"]];
[cell.businessNameLabel setTextColor:[UIColor colorWithRed:1.0/255.0 green:135.0/255.0 blue:68.0/255.0 alpha:1]];
UIFont *customfont = [UIFont fontWithName:@"MyriadPro-Bold" size:16];
[cell.businessNameLabel setFont:customfont];

cell.serviceTypeLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"address"]];
[cell.serviceTypeLabel setTextColor:[UIColor blackColor]];
customfont = [UIFont fontWithName:@"MyriadPro-Regular" size:11];
[cell.serviceTypeLabel setFont:customfont];

cell.businessID = [dataDict objectForKey:@"business_id"];
cell.reviewScoreLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"rating"]];

return cell;

崩溃的原因一定是这个,你试图设置到你的标签的object可能不是NSString。所以最好取StringValue 或格式化它。

【讨论】:

    【解决方案3】:

    我认为这是因为您试图将 NSNumber 设置为文本属性。 text 属性需要一个 NSString。你可以试试:

    cell.reviewScoreLabel.text = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"rating"]];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-05
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2022-11-19
      • 1970-01-01
      相关资源
      最近更新 更多