无法在标签中显示字典对象值

时间:2015-01-21 10:51:00

标签: ios objective-c

我在表格中显示来自Web服务的一系列字典。这是cellForRowAtIndexPath中的代码片段。

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";

3 个答案:

答案 0 :(得分:1)

我认为这是因为您正在尝试将NSNumber设置为文本属性。 text属性需要NSString。你能尝试一下:

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

答案 1 :(得分:0)

试试这个......

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 :(得分:0)

将代码修改为

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或格式化。