2013-01-23 17 views
1

在我的應用程序中,我使用的是一個帶有3個單元的master tableView,取自一個JSON。這3個單元格中的每一個都將連接到另一個tableView,但我不知道如何執行此IF語句。主 - 細節tableviews - 不知道如何/在哪裏做的IF語句

繼承人主表tableView的.m。到目前爲止,我只將1個detailView(tableView)連接到主設備上:

#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 

在此先感謝!

回答

0

你的代碼是好的。 if語句是在正確的地方。

如果你已經鏈接你的原型細胞在故事板的詳細視圖,你不需要實現tableView:DidSelectRowAtIndexPath:

prepareForSegue:sender:被稱爲automaticaly。

+0

是的,謝謝我現在知道。 –

相關問題