數組中有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;
}
在你的陣列 –
開始請出示分配。不要使用字典。不要使用'valueForKey:'。使用'objectForKey:'或一個下標。 – Sulthan
這將有助於查看調試導航器的輸出以幫助識別問題。不過,我個人會在看cellForRowAtIndexPath,並將圍繞valueForKey:@「food_name」包裝一些邏輯,因爲我認爲當「food_list」爲「」時會發生這種情況,因爲您沒有valueForKey food_name。因此,要麼在食品列表中爲密鑰添加空白值,要麼檢查密鑰是否存在。 – Longmang