【发布时间】: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