Search code examples
objective-cif-statementuitableviewmaster-detail

Master - detail tableviews - not sure about how/where to do the IF-statement


In my app I´m using a master tableView with 3 cells, taken from a JSON. Each of these 3 cells will be connected to another tableView but Im not sure how to do this IF-statement.

Heres the .m for the master tableView. So far I only connected 1 detailView(tableView) to the master:

#import "GuideTableViewController.h"
#import "GuideDetailTableViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface GuideTableViewController (){
    NSArray *guide;

}
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@property (weak, nonatomic) IBOutlet UIImageView *imgHeader;


@property (weak, nonatomic) IBOutlet UIButton *btnMap;

@property (weak, nonatomic) IBOutlet UIImageView *ImgTitle;


@end

@implementation GuideTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
     }
    return self;
}


//JSONmetod
- (void) loadJSON{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //code
        NSData *data = [NSData dataWithContentsOfURL:[NSURL   URLWithString:@"https://dl.dropbox.com/u/100670549/test.json"]];

        NSError *error;

        if (data)
        {

            guide = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

            for (NSDictionary *dictionary in guide){
                NSLog([dictionary description]);
            }


        }else
        {
            NSLog(@"Could not load data");
        }




            dispatch_sync(dispatch_get_main_queue(), ^{
                // code
                [self.tableView reloadData];
            });


    });


    }




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //Anropa json

    [self loadJSON];


    //set background
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage   imageNamed:@"background.jpg"]];

    //rounded corners

    [self.tableView.layer setCornerRadius:9.0];



}

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


//TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];


    NSDictionary *dict = [guide objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict valueForKey:@"title"];

    return cell;
}


//To detailView. Started with an IF here.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if ([segue.identifier isEqualToString:@"showStay"]){
        GuideDetailTableViewController *tvc = [segue destinationViewController];
        NSIndexPath *index = sender;
        NSDictionary *dict = [guide objectAtIndex:index.row];

        tvc.stay = dict;
    }
}

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

    [self performSegueWithIdentifier:@"showStay" sender:indexPath];


 }


- (void) viewWillAppear:(BOOL)animated{
    UITableViewCell *cell = [self.tableView     cellForRowAtIndexPath:self.tableView.indexPathForSelectedRow];

    //ta bort markeringen när man går tillbaka till master från detailView.
    [cell setSelected:NO];


    //Hide navbar
    [self.navigationController setNavigationBarHidden:YES];
}


//Show navbar in detailView
-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController setNavigationBarHidden:NO];   
}






@end

Thanks in advance!


Solution

  • Your code is all right. The if-statement is at the right place.

    If you already linked your prototype cell to the detail view in the storyboard you don't need to implement tableView:DidSelectRowAtIndexPath:

    prepareForSegue:sender: is called automaticaly.