【问题标题】:IOS,Tableview Objective C,IOS,Tableview 目标 C,
【发布时间】:2017-02-06 06:53:15
【问题描述】:

一个数组中有 5 个元素,其中 2 个元素有数据,另一个元素是空白的,在 tableview 中它显示两个元素但是当我滚动应用程序崩溃时。我必须显示 menu_name该部分及其工作,但是当我滚动 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;
}

【问题讨论】:

  • 请在您的阵列中显示分配
  • 首先将 JSON 解析为对象。不要使用字典。不要使用valueForKey:。使用objectForKey: 或下标。
  • 查看调试导航器的输出有助于识别问题。但是,我个人会查看 cellForRowAtIndexPath 并将一些逻辑包装在 valueForKey:@"food_name" 周围,因为我认为当“food_list”为“”时您会发现这将失败,因为您没有 valueForKey food_name。因此,要么在 food_list 中为键添加空白值,要么检查键是否存在。

标签: ios objective-c tableview datasource


【解决方案1】:
    return [[[FinalArrayCategory objectAtIndex:0] objectForKey:@"food_list"] count];

将 objectAtIndext 从 0 更改为 IndexPath.section

【讨论】:

    【解决方案2】:

    请试试这个代码..

     -(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{
             cell.lblpice.text=@"";
        }
            return cell;
    }
    

    让我知道它的工作与否。

    编码愉快!!

    【讨论】:

    • @Ethan 至少感谢您的支持或标记为正确答案。 :)
    • 抱歉@Ravi 我有点忙,但感谢代码并帮助了我很多。
    【解决方案3】:
    -(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{
            cell.lblpice.text=@"";
    
        }
    
    
        return cell;
    }
    

    请试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多