dimanche 21 juin 2015

IF Statement; Display value X or X1 in Label based on Y or Y1 value from a tableview cell subtitle

I have a label in my view controller that should display a price (3 or 4.5) based on the value displayed in a subtitle from a tableviewcell.

I want to implement the if else statement in my label (AmountTwo) but struggle to.

If I pick on the slider the a cell it transport it to the mainviewcontroller and shows name and size.

//
//  Cupcake.h
//  SidebarDemo
//
//  Created by Healing on 12/06/15.
//  Copyright (c) 2015 Healing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Cupcake : NSObject

@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *detail;
@property (nonatomic,strong) NSString *imageFile;
@property (nonatomic,strong) NSString *price;
@property (nonatomic,strong) NSString *size;

@property (nonatomic,strong) NSString *nameklein;
@property (nonatomic,strong) NSString *detailklein;
@property (nonatomic,strong) NSString *imageFileklein;
@property (nonatomic,strong) NSString *priceklein;
@property (nonatomic,strong) NSString *sizeklein;

@end

//
//  Cupcake.m
//  SidebarDemo
//
//  Created by Healing on 12/06/15.
//  Copyright (c) 2015 Healing. All rights reserved.
//

#import "Cupcake.h"

@implementation Cupcake

@synthesize name,imageFile,detail,price, size;

@end
//
//  NavigationViewController.h
//  SidebarDemo
//
//  Created by Healing on 12/06/15.
//  Copyright (c) 2015 Healing. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Cupcake.h"

@protocol SliderViewDelegate <NSObject>


@optional
-(void) passSelectedData:(NSMutableArray *)selectedCells;
@end

@interface SliderViewController : UIViewController
{
    id <SliderViewDelegate> delegate;
}

// Delegate property
@property (retain) id delegate;




@end
//
//  NavigationViewController.m
//  SidebarDemo
//
//  Created by Healing on 12/06/15.
//  Copyright (c) 2015 Healing. All rights reserved.
//

#import "SliderViewController.h"
#import "MainViewController.h"

@interface SliderViewController ()


@property (strong, nonatomic) IBOutlet UITableView *myTable;
@property (strong, nonatomic) IBOutlet UILabel *price;



@end

@implementation SliderViewController {
    NSArray *cupcakes;
}
@synthesize delegate;



- (void)viewDidLoad {
    [super viewDidLoad];


    // Create recipe array
    Cupcake *cupcake1 = [Cupcake new];
    cupcake1.name = @"Schoggi Vanille";
    cupcake1.detail = @"Vanille Buttermilch Cake mit Schoggi Buttercreme Topping";
    cupcake1.price = @"4.50";
    cupcake1.size = @"gross";

    cupcake1.imageFile = @"SVG.jpg";

    Cupcake *cupcake2 = [Cupcake new];
    cupcake2.name = @"Schoggi";
    cupcake2.price = @"4.50";
    cupcake2.size = @"gross";
    cupcake2.detail = @"Ein Schoggi Cake mit Schoggi Buttercreme Topping";
    cupcake2.imageFile = @"SG.png";


    Cupcake *cupcake3 = [Cupcake new];
    cupcake3.name = @"Red Velvet";
    cupcake3.detail = @"Leichter Schoggi Cake, rot eingefärbt mit Creamcheese-Weisse Schoggi Topping";
    cupcake3.size = @"gross";
    cupcake3.imageFile = @"RVG.png";
    cupcake3.price = @"4.50";

    Cupcake *cupcake4 = [Cupcake new];
    cupcake4.name = @"Vanille";
    cupcake4.detail = @"Vanille Buttermilch Cake mit Vanille Buttercrème Topping";
    cupcake3.size = @"gross";

    cupcake4.imageFile = @"VG.png";
    cupcake4.price = @"4.50";
    cupcake4.size = @"gross";

    Cupcake *cupcake5 = [Cupcake new];
    cupcake5.name = @"Vanille Schoggi";
    cupcake5.detail = @"Vanille Buttermilch Cake mit Vanille Buttercrème Topping";
    cupcake5.size = @"gross";

    cupcake5.imageFile = @"VS.png";
    cupcake5.price = @"4.50";

    Cupcake *cupcake6 = [Cupcake new];
    cupcake6.nameklein = @"Vanille Schoggi";
    cupcake6.detailklein = @"Vanille Buttermilch Cake mit Vanille Buttercrème Topping";
    cupcake6.sizeklein = @"klein";

    cupcake6.imageFileklein = @"VS.png";
    cupcake6.priceklein = @"3.00";


    cupcakes = [NSArray arrayWithObjects: cupcake1, cupcake2, cupcake3, cupcake4, cupcake5, cupcake6, nil];
}

- (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 cupcakes.count;
}


- (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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    Cupcake *cupcakeInstance = [cupcakes objectAtIndex:indexPath.row];

    cell.imageView.image =[UIImage imageNamed:cupcakeInstance.imageFile];
    cell.detailTextLabel.text = cupcakeInstance.detail;
    cell.textLabel.text = cupcakeInstance.name;



    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSMutableArray *selectedCellArray = [[NSMutableArray alloc] init];
//    for(NSIndexPath *index in [self.myTable indexPathsForSelectedRows])
//    {
//        [selectedCellArray addObject: cupcakes[index.row]];
//    }
//    NSLog(@"SArr : %@",selectedCellArray);
    [self passDataBetweenViewControllers];

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [self passDataBetweenViewControllers];
}

-(void) passDataBetweenViewControllers
{
    NSMutableArray *selectedCellArray = [[NSMutableArray alloc] init];
    for(NSIndexPath *index in [self.myTable indexPathsForSelectedRows])
    {
        [selectedCellArray addObject: cupcakes[index.row]];
    }
    NSLog(@"DArr : %@",selectedCellArray);

    NSLog(@"vc : %@", [self delegate]);

    [[self delegate] passSelectedData:selectedCellArray];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString:@"cupCake"])
    {
        UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;

        MainViewController *navController = [destViewController.viewControllers objectAtIndex:0];

        NSMutableArray *selectedCellArray = [[NSMutableArray alloc] init];
        for(NSIndexPath *index in [self.myTable indexPathsForSelectedRows])
        {
            [selectedCellArray addObject: cupcakes[index.row]];
        }
        NSLog(@"Selected Pass Arr : %@",selectedCellArray);

        //Pass the selected cakes to main View Controller
        navController.cupCakesSelected =selectedCellArray ;

    }

}



@end
//
//  MainViewController.h
//  SidebarDemo
//
//  Created by Healing on 12/06/15.
//  Copyright (c) 2015 Healing. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "SliderViewController.h"
#import "Cupcake.h"


@interface MainViewController : UIViewController <SliderViewDelegate>


@property (strong, nonatomic) IBOutlet UITextField *AmountOne;
@property (strong, nonatomic) IBOutlet UILabel *AmountTwo;
@property (strong, nonatomic) IBOutlet UILabel *AnswerLabel;
- (IBAction)Calculate:(id)sender;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
@property (strong, nonatomic) IBOutlet UITableView *cakeTable;
@property (strong,nonatomic) NSMutableArray *cupCakesSelected;

@property (strong, nonatomic) IBOutlet UILabel *price;

@end
//
//  MainViewController.m
//  SidebarDemo
//
//  Created by Healing on 12/06/15.
//  Copyright (c) 2015 Healing. All rights reserved.
//

#import "MainViewController.h"
#import "SWRevealViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "Cupcake.h"


@interface MainViewController ()



@end


@implementation MainViewController

@synthesize cupCakesSelected, price;



- (void)viewDidLoad
{
    [super viewDidLoad];

    SWRevealViewController *revealViewController = self.revealViewController;

    [(SliderViewController *)self.revealViewController.rearViewController setDelegate:self];

    if ( revealViewController )
    {
        [self.sidebarButton setTarget: self.revealViewController];
        [self.sidebarButton setAction: @selector( revealToggle: )];
        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    }


 }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)Calculate:(id)sender {

    NSString *NumberOneString = [_AmountOne text];
    float NumberOneFloat = [NumberOneString floatValue];

    NSString *NumberTwoString = [_AmountTwo text];
    float NumberTwoFloat = [NumberTwoString floatValue];

    float Answer = NumberOneFloat * NumberTwoFloat;

    NSString *FinalAnswer = [NSString stringWithFormat:@"CHF: %0.2f", Answer];
    [_AnswerLabel setText:FinalAnswer];

    [self.view endEditing:TRUE];
} 


-(void) passSelectedData:(NSMutableArray *)selectedCells
{
    NSLog(@"Cleed");
    self.cupCakesSelected = selectedCells;
    [self.cakeTable reloadData];

}




#pragma mark - Table view data
- (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 cupCakesSelected.count;
}


- (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:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


        Cupcake *cupcakeInstance = [cupCakesSelected objectAtIndex:indexPath.row];

        cell.textLabel.text = cupcakeInstance.name;
        cell.detailTextLabel.text = cupcakeInstance.size;

    return cell;


}
@end

Aucun commentaire:

Enregistrer un commentaire