我是 ios 的初学者,正在构建我的第一个应用程序......所以对我来说放轻松:-)
我正在尝试在表格视图单元格中显示来自 Web 服务的结果。但是我的while循环条件失败了。我将 nslog 移到了 while 循环中,它清楚地打印了数组中的对象,正如您所期望的那样。但是,我相信当它结束时会有一些问题。
有没有人对它为什么失败有任何想法?
基本上我正在尝试获取“配方名称”和“smallImageUrls; 从 Web 服务结果并在 tableview 单元格中打印。
我知道我的 tableview 单元格不会按原样正确打印。我稍后会解决这个问题。可能将 url 提取到一个数组中,然后在 tableviewcell 方法中使用该数组。
非常感谢任何人可以提供的任何帮助。我只是觉得它很奇怪,因为它显然正在通过 while 循环而没有任何问题。:-S
没有真正的错误信息只是:
self SearchedYummlyViewController * 0x12b39570 匹配计数 int 40
在底部窗格的左侧。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.searchYummlyRecipeResults = [NSJSONSerialization JSONObjectWithData:self.yummlyRecipesNSData options:nil error:nil];
self.tempYummlyResultsMatches = [[NSMutableArray alloc]init];
int matchesCount = 0;
while([self.searchYummlyRecipeResults[@"matches"]objectAtIndex:matchesCount]){
[self.tempYummlyRecipeMatch addObject:[[self.searchYummlyRecipeResults[@"matches"]objectAtIndex:matchesCount]objectForKey:@"recipeName"]];
[self.tempYummlyRecipeMatch addObject:[[self.searchYummlyRecipeResults[@"matches"]objectAtIndex:matchesCount]objectForKey:@"smallImageUrls"]];
NSLog(@"tempYummlyRecipeMatch array's contents: %@", self.tempYummlyRecipeMatch);
matchesCount++;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSString *recipeString = self.tempYummlyRecipeMatch[indexPath.row];
cell.textLabel.text = recipeString;
//The below is code on how to add a image from a web service to a cell. I dont think that will work but my code cant make it down there yet so I'll tackle that after while loop issue.
NSURL *url = [NSURL URLWithString:self.tempYummlyRecipeMatch[indexPath.row+1];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
cell.imageView.image = image;
return cell;
}
下面的 Web 服务输出示例:
Recipe List: {
attribution = {
html = "<a href='http://www.yummly.com/recipes/onion-soup'>onion soup recipes</a> search powered by <img src='http://static.yummly.com/api-logo.png'/>";
logo = "http://static.yummly.com/api-logo.png";
text = "onion soup recipes: search powered by Yummly";
url = "http://www.yummly.com/recipes/onion-soup";
};
criteria = {
allowedIngredients = (
);
excludedIngredients = (
);
facetFields = (
);
maxResults = 6;
requirePictures = 0;
resultsToSkip = 0;
terms = (
onion,
soup
);
};
facetCounts = {
};
matches = (
{
attributes = {
};
flavors = {
bitter = "0.1666666666666667";
meaty = "0.3333333333333333";
piquant = 0;
salty = "0.5";
sour = "0.3333333333333333";
sweet = "0.5";
};
id = "French-onion-soup-sandwiches-309090";
ingredients = (
"olive oil",
"sea salt",
"fresh thyme leaves",
"granulated sugar",
"yellow onions",
"unsalted butter",
"beef broth",
"cracked black pepper",
"gruyere cheese",
bread
);
rating = 0;
recipeName = "French Onion Soup Sandwiches";
smallImageUrls = (
"http://i.yummly.com/French-onion-soup-sandwiches-309090-273265.s.jpg"
);
sourceDisplayName = "Joy the Baker";
totalTimeInSeconds = 0;
},
{
attributes = {
course = (
"Main Dishes"
);
};
flavors = {
bitter = 1;
meaty = "0.1666666666666667";
piquant = 0;
salty = 1;
sour = "0.1666666666666667";
sweet = "0.3333333333333333";
};
id = "Awesome-Slow-Cooker-Pot-Roast-Allrecipes";
ingredients = (
"condensed cream of mushroom soup",
"onion soup mix",
"pot roast",
water
);
rating = "4.69";
recipeName = "Awesome Slow Cooker Pot Roast";
smallImageUrls = (
"http://i2.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-2.s.png",
"http://i.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-58919.s.png"
);
sourceDisplayName = AllRecipes;
totalTimeInSeconds = 29400;
},
{
attributes = {
course = (
Soups,
Appetizers
);
cuisine = (
French
);
holiday = (
Thanksgiving
);
};
flavors = {
bitter = "0.1666666666666667";
meaty = "0.1666666666666667";
piquant = 0;
salty = "0.1666666666666667";
sour = "0.1666666666666667";
sweet = "0.1666666666666667";
};
id = "French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364";
ingredients = (
"stick butter",
"french bread",
"yellow onion",
"low sodium chicken broth",
"gruyere cheese",
"minced garlic",
"dry white wine",
"worcestershire sauce",
"beef broth"
);
rating = 0;
recipeName = "French Onion Soup";
smallImageUrls = (
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1512.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1199.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-220.s.jpg"
);
sourceDisplayName = "The Pioneer Woman";
totalTimeInSeconds = 0;
},
{
attributes = {
course = (
Soups
);
};
flavors = "<null>";
id = "Curried-sweet-potato-soup-333086";
ingredients = (
broth,
salt,
"sweet potatoes",
"lite coconut milk",
onion,
"coconut oil",
"curry powder",
garlic,
spinach,
lime,
"chopped cilantro",
"red pepper flakes",
"brown sugar",
peas
);
rating = 0;
recipeName = "Curried Sweet Potato Soup";
smallImageUrls = (
"http://i.yummly.com/Curried-sweet-potato-soup-333086-295006.s.jpg"
);
sourceDisplayName = "Camille Styles";
totalTimeInSeconds = 0;
},
{
attributes = {
};
flavors = "<null>";
id = "French-onion-soup-grilled-cheese-308496";
ingredients = (
"unsalted butter",
"kosher salt",
"sweet onions",
"unsalted beef stock",
pepper,
"gruyere cheese",
"dry sherry",
"italian bread",
"fresh thyme"
);
rating = 0;
recipeName = "French Onion Soup Grilled Cheese";
smallImageUrls = (
"http://i.yummly.com/French-onion-soup-grilled-cheese-308496-272505.s.jpg"
);
sourceDisplayName = "The Kitchn";
totalTimeInSeconds = 0;
},
{
attributes = {
};
flavors = {
bitter = 1;
meaty = 1;
piquant = 0;
salty = 1;
sour = 1;
sweet = "0.8333333333333334";
};
id = "Vidalia-onion-soup-with-wild-rice-and-blue-cheese-305366";
ingredients = (
"vidalia onions",
"chicken stock",
herb,
pepper,
"wild rice",
"unsalted butter",
baguette,
salt,
"extra virgin olive oil",
other
);
rating = 0;
recipeName = "Vidalia Onion Soup with Wild Rice and Blue Cheese";
smallImageUrls = (
"http://i.yummly.com/Vidalia-onion-soup-with-wild-rice-and-blue-cheese-305366-268824.s.jpg"
);
sourceDisplayName = "Smitten Kitchen";
totalTimeInSeconds = 0;
},
tempYummlyRecipeResults nslog 示例
tempYummlyRecipeMatch array's contents: (
"French Onion Soup Sandwiches",
(
"http://i.yummly.com/French-onion-soup-sandwiches-309090-273265.s.jpg"
)
)
2013-02-27 22:13:34.334 CustomTableView[435:11303] tempYummlyRecipeMatch array's contents: (
"French Onion Soup Sandwiches",
(
"http://i.yummly.com/French-onion-soup-sandwiches-309090-273265.s.jpg"
),
"Awesome Slow Cooker Pot Roast",
(
"http://i2.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-2.s.png",
"http://i.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-58919.s.png"
)
)
2013-02-27 22:13:34.335 CustomTableView[435:11303] tempYummlyRecipeMatch array's contents: (
"French Onion Soup Sandwiches",
(
"http://i.yummly.com/French-onion-soup-sandwiches-309090-273265.s.jpg"
),
"Awesome Slow Cooker Pot Roast",
(
"http://i2.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-2.s.png",
"http://i.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-58919.s.png"
),
"French Onion Soup",
(
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1512.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1199.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-220.s.jpg"
)
)
2013-02-27 22:13:34.335 CustomTableView[435:11303] tempYummlyRecipeMatch array's contents: (
"French Onion Soup Sandwiches",
(
"http://i.yummly.com/French-onion-soup-sandwiches-309090-273265.s.jpg"
),
"Awesome Slow Cooker Pot Roast",
(
"http://i2.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-2.s.png",
"http://i.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-58919.s.png"
),
"French Onion Soup",
(
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1512.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1199.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-220.s.jpg"
),
"Curried Sweet Potato Soup",
(
"http://i.yummly.com/Curried-sweet-potato-soup-333086-295006.s.jpg"
)
)
2013-02-27 22:13:34.335 CustomTableView[435:11303] tempYummlyRecipeMatch array's contents: (
"French Onion Soup Sandwiches",
(
"http://i.yummly.com/French-onion-soup-sandwiches-309090-273265.s.jpg"
),
"Awesome Slow Cooker Pot Roast",
(
"http://i2.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-2.s.png",
"http://i.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-58919.s.png"
),
"French Onion Soup",
(
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1512.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1199.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-220.s.jpg"
),
"Curried Sweet Potato Soup",
(
"http://i.yummly.com/Curried-sweet-potato-soup-333086-295006.s.jpg"
),
"French Onion Soup Grilled Cheese",
(
"http://i.yummly.com/French-onion-soup-grilled-cheese-308496-272505.s.jpg"
)
)
2013-02-27 22:13:34.335 CustomTableView[435:11303] tempYummlyRecipeMatch array's contents: (
"French Onion Soup Sandwiches",
(
"http://i.yummly.com/French-onion-soup-sandwiches-309090-273265.s.jpg"
),
"Awesome Slow Cooker Pot Roast",
(
"http://i2.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-2.s.png",
"http://i.yummly.com/Awesome-Slow-Cooker-Pot-Roast-Allrecipes-58919.s.png"
),
"French Onion Soup",
(
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1512.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-1199.s.jpg",
"http://i.yummly.com/French-Onion-Soup-The-Pioneer-Woman-Cooks-_-Ree-Drummond-41364-220.s.jpg"
),
"Curried Sweet Potato Soup",
(
"http://i.yummly.com/Curried-sweet-potato-soup-333086-295006.s.jpg"
),
"French Onion Soup Grilled Cheese",
(
"http://i.yu