This question already has an answer here:
how can i show pop-up
with recipes selected(name and price) when click button?
I hope that you can help me!
Thanks everybody in advance!
this is my code:
#import "RecipeViewController.h"
#import "Recipe.h"
@interface RecipeViewController ()
@end
@implementation RecipeViewController {
NSArray *recipes;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = @"Recipe Book";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
// Create recipe array
Recipe *recipe1 = [Recipe new];
recipe1.name = @"Mushroom Risotto";
recipe1.detaill = 20;
recipe1.imageFile = @"mushroom_risotto.jpg";
Recipe *recipe2 = [Recipe new];
recipe2.name = @"Egg Benedict";
recipe2.detaill = 30;
recipe2.imageFile = @"egg_benedict.jpg";
Recipe *recipe3 = [Recipe new];
recipe3.name = @"Full Breakfast";
recipe3.detaill = 20;
recipe3.imageFile = @"full_breakfast.jpg";
Recipe *recipe4 = [Recipe new];
recipe4.name = @"Hamburger";
recipe4.detaill = 30;
recipe4.imageFile = @"hamburger.jpg";
Recipe *recipe5 = [Recipe new];
recipe5.name = @"Ham and Egg Sandwich";
recipe5.detaill = 10;
recipe5.imageFile = @"ham_and_egg_sandwich.jpg";
recipes = [NSMutableArray arrayWithObjects:recipe1, recipe2, recipe3, recipe4, recipe5, nil];
UIButton *btnAlert = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// btnAlert.frame = CGRectMake(20, self.view.frame.size.height - 35 + 5, self.view.frame.size.width - 20 - 20, 50 - 10);
[btnAlert setTitle:@"Get_answer" forState:UIControlStateNormal];
[btnAlert addTarget:self action:@selector(btnAlertTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnAlert];
// Remove table cell separator
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
// Assign our own backgroud for the view
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common_bg"]];
self.tableView.backgroundColor = [UIColor clearColor];
// Add padding to the top of the table view
UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
self.tableView.contentInset = inset;
}
//-(void)btnAlertTapped:(id)sender {
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You selected" message:[[self getSelections] componentsJoinedByString:@", "] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
// [alert show];
//}
//
//-(NSArray *)getSelections {
// NSMutableArray *selections = [[NSMutableArray alloc] init];
//
// for(NSIndexPath *indexPath in recipes) {
// [selections recipe1.name,recipes:[recipes objectAtIndex:indexPath.row]];
// }
//
// return selections;
//}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return recipes.count;
}
- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
NSInteger rowIndex = indexPath.row;
UIImage *background = nil;
if (rowIndex == 0) {
background = [UIImage imageNamed:@"cell_top.png"];
} else if (rowIndex == rowCount - 1) {
background = [UIImage imageNamed:@"cell_bottom.png"];
} else {
background = [UIImage imageNamed:@"cell_middle.png"];
}
return background;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];
UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;
UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = [NSString stringWithFormat:@"%d", recipe.detaill,"€"];
//recipeDetailLabel.text = recipe.detaill;
// Assign our own background image for the cell
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
RecipeViewController.h
#import <UIKit/UIKit.h>
@interface RecipeViewController : UITableViewController
@end
Recipe.m
#import "Recipe.h"
@implementation Recipe
@synthesize name;
@synthesize detaill;
@synthesize imageFile;
@end
Recipe.h
#import <Foundation/Foundation.h>
@interface Recipe : NSObject
@property (nonatomic, strong) NSString *name; // name of recipe
//@property (nonatomic, strong) NSString *detail; // recipe detail
@property NSInteger *detaill; // recipe detail
@property (nonatomic, strong) NSString *imageFile; // image filename of recipe
@property int rowSelected;
@end
Aucun commentaire:
Enregistrer un commentaire