1

在我的应用程序中,我使用了一个带有 3 个单元格的主 tableView,取自 JSON。这 3 个单元格中的每一个都将连接到另一个 tableView,但我不确定如何执行此 IF 语句。

这是主tableView的.m。到目前为止,我只将 1 个 detailView(tableView) 连接到 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

提前致谢!

4

1 回答 1

0

你的代码没问题。if 语句在正确的位置。

如果您已经将原型单元链接到情节提要中的详细视图,则无需实现tableView:DidSelectRowAtIndexPath:

prepareForSegue:sender:被自动调用。

于 2013-03-05T18:02:59.060 回答