我是ios的初学者并且正在构建我的第一个应用程序......所以对我很轻松: - )
我正在尝试在tableview单元格中显示来自Web服务的结果。但是我的while循环条件失败了。我将nslog移动到while循环中,它可以清楚地打印出数组中的对象。但是,当它结束时,我认为有些不对劲。
有没有人对它失败的原因有任何想法?
基本上我想要'食谱名'和'smallImageUrls;从Web服务结果并在tableview单元格中打印。
我知道我的tableview单元格不能正常打印。我稍后会解决这个问题。可能会将url提取到一个数组中,然后在tableviewcell方法中使用该数组。
非常感谢任何人提供的任何帮助。我发现它很奇怪,因为它显然是通过while循环没有任何问题。 :-S
没有真正的错误讯息:
self SearchedYummlyViewController * 0x12b39570 matchesCount 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;
}
下面的webservice输出示例:
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
答案 0 :(得分:0)
在connectionDidFinishLoading
的末尾,您应该重新加载表格视图。
[self.tableView reloadData];
只是更新您的数组不会立即更新您的表格视图。
编辑:您是对的,您的while
循环条件不正确。目前,它仅检查索引matchesCount
处的对象是否不是nil
(有点类似于while(object)
或while(object != nil)
)。我认为你要找的是数组中的对象数是否大于matchesCount
,以防止数组索引超出范围异常。为此,您需要使用while([self.searchYummlyRecipeResults[@"matches"] count] > matchesCount)
。