I created a menu app in Xcode. I have retrieved my menus from a JSON file.
"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",
},]
In my json.h I have this line code which I will use in in my UITableView:
-(void)dataRequestCompletedWithJsonObject:(id)jsonObject;
Then I call this method in my 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
When I run it, I will get only the menu of Sunday in my table view. It works fine.
Now I want to use segment control or other tool so that when users choose "sun" they see only Sunday list and so on. How can I do that?
My cell.
- (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;
}