2016-09-28 82 views
-1

數組中有5個元素,其中2個元素有數據,另外一個元素是空白的,在tableview中它顯示了這兩個元素,但是當我滾動應用程序crash.i時,必須顯示menu_name在部分和它的工作,但當我滾動tableview應用程序崩潰,因爲任何人都可以給我解決方案?IOS,Tableview Objective C,

下面是我在tableview中

"status":"true", 
    "message":"food point menu available", 
    "data":[ 
    { 
     "menu_id":"9", 
     "menu_name":"Morning Menu", 
     "food_list":[ 
     { 
      "food_name":"Brigadeiros", 
      "foodtype":"Pastas", 
      "food_per_person":"20", 
      "food_bonus":"packing" 
    }, 
    { 
     "food_name":"Cachaca", 
     "foodtype":"Pastas", 
     "food_per_person":"30", 
     "food_bonus":"packing" 
    } 
    ] 
    }, 
    { 
    "menu_id":"10", 
    "menu_name":"Afternoon Menu", 
     "food_list":[ 
     { 
      "food_name":"Quindim", 
      "foodtype":"Pastas", 
      "food_per_person":"30", 
      "food_bonus":"packing" 
     }, 
     { 
      "food_name":"Cachaca", 
      "foodtype":"Pastas", 
      "food_per_person":"30", 
      "food_bonus":"packing" 
     } 
    ] 
    }, 
    { 
    "menu_id":"12", 
    "menu_name":"Evening Menu", 
    "food_list":"" 
    }, 
    { 
    "menu_id":"17", 
    "menu_name":"MenuSegundaFeira", 
    "food_list":"" 
    }, 
    { 
    "menu_id":"18", 
    "menu_name":"MenuTercaFeira", 
    "food_list":"" 
    } 
    ] 
    } 

顯示的響應,並且這是一個我已經寫在我的應用程序

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

{ 
    if([FinalArrayCategory count]>0) { 
     return [[[FinalArrayCategory objectAtIndex:0] objectForKey:@"food_list"] count]; 
    } 
    else 
    { 
     return 0; 
    } 
} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if([FinalArrayCategory count]>0) { 
     return [[FinalArrayCategory valueForKey:@"menu_name"] count]; 
    } 
    else 
    { 
     return 0; 
    } 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    NSString *sectionName; 
    sectionName =[[FinalArrayCategory valueForKey:@"menu_name"] objectAtIndex:section]; 
    return sectionName; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.lblpice.text=[[[[FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:@"food_list"] valueForKey:@"food_name"]objectAtIndex:indexPath.row]; 

    // cell.lblType.text=[[FinalArrayCategory valueForKey:@"category_name"] objectAtIndex:indexPath.row]; 



    // cell.lblBonus.text=[[FinalArrayCategory valueForKey:@"food_bonus"] objectAtIndex:indexPath.section]; 

    // cell.lblFoodName.text=[[FinalArrayCategory valueForKey:@"menu_name"] objectAtIndex:indexPath.section]; 

    // 

    // NSString *pickURL=[[FinalArrayCategory valueForKey:@"food_image"] objectAtIndex:indexPath.section]; 

    // [cell.lblImage sd_setImageWithURL:[NSURL URLWithString:pickURL] placeholderImage:[UIImage imageNamed:@"itemplaceholder.jpg"] options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) { 

    // } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 

    // }]; 
    return cell; 
} 
+0

在你的陣列 –

+0

開始請出示分配。不要使用字典。不要使用'valueForKey:'。使用'objectForKey:'或一個下標。 – Sulthan

+0

這將有助於查看調試導航器的輸出以幫助識別問題。不過,我個人會在看cellForRowAtIndexPath,並將圍繞valueForKey:@「food_name」包裝一些邏輯,因爲我認爲當「food_list」爲「」時會發生這種情況,因爲您沒有valueForKey food_name。因此,要麼在食品列表中爲密鑰添加空白值,要麼檢查密鑰是否存在。 – Longmang

回答

1

請試試這個代碼的代碼..

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return [[[FinalArrayCategory objectAtIndex:section] objectForKey:@"food_list"] count]; 
    } 
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 

     return [FinalArrayCategory count]; 

    } 

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
    { 
     NSString *sectionName; 
     sectionName =[[FinalArrayCategory objectAtIndex:section] objectForKey:@"menu_name"]; 
     return sectionName; 
    } 

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if ([[[FinalArrayCategory objectAtIndex:indexPath.section] objectForKey:@"food_list"] iskindOfClass:[NSArray class]]) { 
     NSDictionary *dictData=[[[FinalArrayCategory objectAtIndex:indexPath.section] objectForKey:@"food_list"] objectAtIndex:indexPath.row]; 
     cell.lblpice.text=[dictData objectForKey:@"food_name"]; 

    }else{ 
     [email protected]""; 
    } 
     return cell; 
} 

讓我知道它的工作與否。

快樂編碼!

+0

@不勝感激,讚揚或標記爲正確答案。 :) –

+0

對不起@Ravi我有點忙,但感謝代碼,並幫助我很多。 – Ethan

+0

@不用了。最受歡迎的。 ;) –

0
return [[[FinalArrayCategory objectAtIndex:0] objectForKey:@"food_list"] count]; 

變化objectAtIndext從0到IndexPath.section

-1
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    MenuListCELL *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if([FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:@"food_list"].count>0){ 

     NSString *foodName =[[[[FinalArrayCategory objectAtIndex:indexPath.section]objectForKey:@"food_list"] valueForKey:@"food_name"]objectAtIndex:indexPath.row]; 
     if(foodName){ 

      cell.lblpice.text=foodName; 
     } 



    }else{ 
     [email protected]""; 

    } 


    return cell; 
} 

請JSON解析成對象試試這個