IOS,Tableview Objective C,

问题描述:

数组中有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

请试试这个代码的代码..

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

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

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

变化objectAtIndext从0到IndexPath.section

-(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解析成对象试试这个