2013-02-27 55 views
1

我是一个初学者到iOS和我建立我的第一个应用程序......这样下去容易对我:-)条件while循环无法正常工作,无法填充的tableview细胞

我试图显示结果来自tableview单元格中的web服务。然而我的while循环条件失败了。我将nslog移入了while循环,并且它清楚地按照您的预期打印数组中的对象。然而,当我相信它结束时会出现错误。

有没有人有任何想法,为什么它失败?

基本上我试图得到'食谱名'和'smallImageUrls;从Web服务结果中打印出来并打印到tableview单元格中。

我知道我的tableview单元格不会按原样正确打印。我会解决这个问题。可能将网址提取到一个数组中,然后在tableviewcell方法中使用该数组。

任何人都可以提供的援助是非常感谢。我只是觉得很奇怪,因为它明显经历了while循环而没有任何问题。 :-S

没有真正的错误信息只是:

自SearchedYummlyViewController * 0x12b39570 matchesCount诠释40

在底部窗格的左侧部分。下面的web服务输出的

- (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; 
} 

例:tempYummlyRecipeResults的

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; 
     }, 

例的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

在你connectionDidFinishLoading结束后,你应该重新载入你的表视图。

[self.tableView reloadData]; 

只是更新您的阵列并不会立即更新您的表格视图。

编辑:你是对的,你的while循环条件是不正确的。目前,它仅检查索引为matchesCount的对象是不是nil(有点类似于while(object)while(object != nil))。我认为你要找的是如果数组中的对象数量大于matchesCount,以防止数组索引越界异常。为此,您需要使用while([self.searchYummlyRecipeResults[@"matches"] count] > matchesCount)

+0

嗨neilvillareal - 感谢您的回应。但是我觉得'while循环'中的实际语句有些问题。我改变了循环while(matchesCount <10)。我现在可以成功通过while循环。配方名称显示在tableview单元格中。所以我会猜测while循环中的实际表达式有问题。我离开了它,但我正在重新加载tableview。没关系。我省略了。 – Anthony 2013-02-28 10:24:54

+0

@Anthony我刚编辑我的答案。如果解决了你的问题,你可以试试吗? – neilvillareal 2013-02-28 12:51:13

+0

非常感谢!工作过一种享受。 – Anthony 2013-02-28 19:42:57