Objective-C Table 画面遷移 基本

Table 画面遷移基本例です。

TableViewController.h

#import <UIKit/UIKit.h>


@interface TableViewController : UITableViewController


@end

TableViewController.m

#import "TableViewController.h"

#import "ViewController.h"


@interface TableViewController () {

    

    NSArray *_items;


}



@end


@implementation TableViewController


- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

    // 表示データの設定

//    _items = @[@"BMW01",

//               @"BMW02",

//               @"BMW03",

//               @"BMW04",

//               @"BMW05"];

    

    // 表示データの設定(Plist1

//    NSBundle *bnd = [NSBundle mainBundle];

//    NSString *prs = [bnd pathForResource:@"Items"

//                                ofType:@"plist"];

//    _items = [NSArray arrayWithContentsOfFile:prs];

    

//    NSLog(@"Plist %@ %@ %@", _items, bnd, prs);

    

    // 表示データの設定(Plist2

//    NSBundle *bnd = [NSBundle mainBundle];

//    NSString *prs = [bnd pathForResource:@"Items2"

//                                  ofType:@"plist"];

//    _items = [NSArray arrayWithContentsOfFile:prs];

    

    // 表示データの設定(Plist3

    NSBundle *bnd = [NSBundle mainBundle];

    NSString *prs = [bnd pathForResource:@"Items3"

                                  ofType:@"plist"];

    _items = [NSArray arrayWithContentsOfFile:prs];

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


// 設定(列)

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

// #warning Potentially incomplete method implementation.

    // Return the number of sections.

    return 1;

}


// 設定(行数)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

// #warning Incomplete method implementation.

    // Return the number of rows in the section.

    

    // 要素の数 配列数 Plist1

//    return [_items count];

    

    // 要素の数 配列数(Plist2

    //return [_items[0] count];

    

    // 要素の数 配列数(Plist3

    return [_items count];

}


// 設定(セル)

- (UITableViewCell *)tableView:(UITableView *)tableView

         cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    // セルオブジェクト取得

    UITableViewCell *cell =

    [tableView dequeueReusableCellWithIdentifier:@"Cell"

                                    forIndexPath:indexPath];

    

    // Configure the cell...

    

    // データ表示(Plist1)設定(セル)引数:indexPath

    //cell.textLabel.text = _items[indexPath.row];

    

    // データ表示(Plist2

//    NSDictionary *dic = _items[0];

//    NSString *str = [NSString stringWithFormat:@"KEY%02ld",(long)indexPath.row];

//    cell.textLabel.text = dic[str];

    

    

    // 表示データの設定(Plist3

    NSDictionary *dic = _items[indexPath.row];

    

    // テキストラベルに代入

    cell.textLabel.text = dic[@"Name"];

    cell.detailTextLabel.text = dic[@"Note"];

    

    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 - Navigation


// ViewController に代入

// In a storyboard-based application, you will often want to do a little preparation before navigation

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

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

    

    // セグエ判定

    if ([segue.identifier isEqualToString:@"Segue01"]){

        

        // セルの情報の取得

        NSIndexPath *lp = self.tableView.indexPathForSelectedRow;

        NSString *str = _items[lp.row][@"Name"];

        

        ViewController *vc = (ViewController *)segue.destinationViewController;

        

        vc.mssage = str;

    }

}


@end

ViewController.h

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property (weak, nonatomic) NSString *mssage;


@end

ViewController.m

#import "ViewController.h"


@interface ViewController ()


@property (weak, nonatomic) IBOutlet UILabel *lbMessage;


@end


@implementation ViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    // 引き渡されたデータの表示

    //self.lbMessage.text = _mssage;

    

    self.lbMessage.text = self.mssage;


}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#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

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end

GitHub TableDetail3

  

目 次