我有一个表格视图,其中每个单元格都希望从Web服务中填充数据。每个单元格应显示对象的名称,价格和品牌名称。我的名字很好,但价格和品牌名称并不容易。我检查了网络服务,它有价格和品牌名称,所以它肯定是我的代码的问题。到目前为止看起来如何:
for (NSDictionary *objItem in resultsArray)
{
NSString *currentItemName = [objectTitlesArray objectAtIndex:indexPath.row];
if ([currentItemName isEqualToString:objItem[@"title"]])
{
cell.nameLabel.text = currentItemName;
cell.priceLabel.text = objItem[@"price"];
cell.brandLabel.text = objItem[@"brands"];
}
}
objectTitlesArray是我单独制作的一个数组,其中包含返回的对象的名称,这就是为什么我可以轻松获取名称的原因。
这里是resultsArray里面的内容:
<__NSArrayI 0x8cb9900>(
{
asins = (
);
"best_page" = {
currency = USD;
deeplink = "http://www.textbooks.com/iPhone-Book-Covers-iPhone-4S-iPhone-4-and-iPhone-3GS/9780321832764/Scott-Kelby.php";
description = "\"Excellent Marketplace listings for \"\"iPhone Book: Covers iPhone 4S, iPhone 4, and iPhone 3GS\"\" by Scott Kelby starting as low as $1.99!\"";
"image_url" = "http://images.textbooks.com/TextbookInfo/Covers/0321832760.gif";
"in_stock" = 1;
"live_price_url" = "http://api.invisiblehand.co.uk/v1/pages/live_price?url=http%3A%2F%2Fwww.textbooks.com%2FiPhone-Book-Covers-iPhone-4S-iPhone-4-and-iPhone-3GS%2F9780321832764%2FScott-Kelby.php";
"original_url" = "http://www.textbooks.com/iPhone-Book-Covers-iPhone-4S-iPhone-4-and-iPhone-3GS/9780321832764/Scott-Kelby.php";
pnp = "3.99";
price = "24.99";
"price_confidence" = low;
region = us;
"retailer_name" = "Textbooks.com";
title = "iPhone Book: Covers iPhone 4S, iPhone 4, and iPhone 3GS";
};
brands = (
Pearson
);
categories = (
);
eans = (
9780321832764
);
id = 10a9fb4e3868f5289dc0d53af80ae86a;
"image_url" = "http://images.textbooks.com/TextbookInfo/Covers/0321832760.gif";
isbns = (
0321832760
);
models = (
);
mpns = (
0321832760
);
"number_of_pages" = 1;
resource = "/products/10a9fb4e3868f5289dc0d53af80ae86a";
title = "iPhone Book: Covers iPhone 4S, iPhone 4, and iPhone 3GS";
upcs = (
);
},
感谢所有帮助,并提前感谢。
编辑:
将我的代码更改为:
for (NSDictionary *objItem in resultsArray)
{
NSString *currentItemName = [objectTitlesArray objectAtIndex:indexPath.row];
if ([currentItemName isEqualToString:objItem[@"title"]])
{
if (cell.priceLabel.text != (id)[NSNull null] && cell.priceLabel.text.length != 0 && cell.brandLabel.text != (id)[NSNull null] && cell.brandLabel.text.length != 0)
{
cell.nameLabel.text = currentItemName;
NSDictionary *bestPageDictionary = objItem[@"best_page"];
cell.priceLabel.text = bestPageDictionary[@"price"];
NSArray *brandsArray = objItem[@"brands"];
cell.brandLabel.text = [brandsArray firstObject];
}
}
}
现在我得到NSInvalidArgumentException&#39;,原因:&#39; - [__ NSCFNumber长度]:无法识别的选择器发送到实例0x8c98c00&#39;
答案 0 :(得分:1)
您的词典结构与您尝试摆脱它的数据不匹配。
brands
是一个数组,而不是一个字符串。它碰巧包含一个字符串元素,所以你只需要抓住它。
NSArray *brandsArray = objItem[@"brands"];
cell.brandLabel.text = [brandsArray firstObject];
price
不在顶级词典中,而是嵌套在其中的另一个词典中。
NSDictionary *bestPageDictionary = objItem[@"best_page"];
cell.priceLabel.text = bestPageDictionary[@"price"];
修改看起来价格实际上可能是一个数字,即使它看起来像是一个字符串。试试这个:
NSDictionary *bestPageDictionary = objItem[@"best_page"];
NSNumber *price = bestPageDictionary[@"price"];
cell.priceLabel.text = [NSString stringWithFormat:@"%@", price];