2013-02-27 74 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