我在 Xcode 中创建了一个菜单应用程序。我从JSON 文件中检索了我的菜单。
"Mon": [
{
"Type" : "Breakfast",
"Name":"Sweet Potato Breakfast Taquitos",
"Price" : "OMR 1.500",
},
{
"Type" : "Dinner",
"Name":"Fall Sausage Skillet Dinner",
"Price" : "OMR 1.300",
}
],
"Sun": [
{
"Type" : "Breakfast",
"Name":"Breakfast Casserole",
"Price" : "1500",
},
{
"Type" : "Breakfast",
"Name":"Breakfast Tart",
"Price" : "2000",
},]
在我的 json.h 中,我有这行代码,我将在我的 UITableView 中使用:
-(void)dataRequestCompletedWithJsonObject:(id)jsonObject;
然后我在我的 UITableView 中调用这个方法。
///Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MenuDetail *detail = [[MenuDetail alloc] initWithNibName:@"MenuDetail" bundle:nil];detail.menu= [menus objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detail animated:YES];
}
-(void)loadMenus
{
NSString* url = @"http://gutech.net63.net/GUeatJson";
Json *myJsonParser = [[Json alloc] init];
[myJsonParser startLoadingObjectWithUrl:url andDelegate:self];
}
-(void)dataRequestCompletedWithJsonObject:(id)jsonObject
{
NSDictionary *menuDictionary = (NSDictionary*)jsonObject; //to check
NSArray* menuArray = (NSArray*)[menuDictionary objectForKey:@"Sun"]; //will see only sun list
self.menus = [[NSMutableArray alloc] init];
for (NSDictionary* dic in menuArray) {
Menu *menu = [[Menu alloc] init];
menu.name = [dic objectForKey:@"Name"];//titlle
menu.image = [dic objectForKey:@"Image"];//yello box url image
menu.price = [dic objectForKey:@"Price"]; //yellow value
[menus addObject:menu];
}
[self.tableView reloadData];
}
@end
当我运行它时,我只会在我的表格视图中获得周日的菜单。它工作正常。
现在我想使用段控制或其他工具,这样当用户选择“太阳”时,他们只会看到星期天列表等等。我怎样才能做到这一点?
我的牢房。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"MenuCell" owner:self options:nil];
for(id currentObject in objects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = ( MenuCell *)currentObject;
break;
}
}
}
Menu *menu = [menus objectAtIndex:indexPath.row];
[cell setDetailsWithMenu:menu];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
// Configure the cell...
return cell;
}