LoginSignup
10
10

More than 5 years have passed since last update.

UIViewControllerでUITableViewを使うときのテンプレートファイルをXcodeに追加する

Last updated at Posted at 2014-02-21

UITableViewControllerというテンプレートファイルがありますが、
個人的には使ったことがありません。。
いつも結局は、UIViewControllerにUITableViewを持たせてコーディングしてます。
いつもいつも作るのは面倒なので、
shu223さんの記事Xcodeのファイルテンプレートを自作するを参考にして、自分で作ってみました。

自作テンプレートファイル置き場を作成する

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/File Templatesディレクトリにある、 Cocoa TouchをコピーしてCocoa Touchと同じ階層に My Cocoa Touchというディレクトリを作ります。

テンプレート情報を登録する

次に、Tempate情報を登録するために、 My Cocoa Touch/Objective-C class.xctemplate/TemplateInfo.plistを開きます。

TemplateInfo.plistは以下のような構造になっています。
(画像はPlistEditProで開いたときのものです)

このplistの Root/Optionsに以下のDictionaryを追加します。

TemplateInfo.plistのOptionsに追加するDictionary
        <dict>
            <key>Default</key>
            <string>false</string>
            <key>Identifier</key>
            <string>TableView</string>
            <key>Name</key>
            <string>TableView</string>
            <key>RequiredOptions</key>
            <dict>
                <key>cocoaTouchSubclass</key>
                <array>
                    <string>UIViewController</string>
                </array>
            </dict>
            <key>Type</key>
            <string>checkbox</string>
        </dict>

PlistEdirProで見るとこんな感じです。

テンプレートファイルを作成する

Optionsに定義した内容に対する、テンプレートファイルを作成します。
My Cocoa Touch/Objective-C class.xctemplate/UIViewControllerをコピーして
UIViewControllerTableViewというディレクトリを作ります。

注意)このディレクトリの名前は、TemplateInfo.plistと関係しています。今回の場合で言うと、
参照されるディレクトリ名 = UIViewController(cocoaTouchSubclassに定義した) + TableView(Nameに定義した)となっているようです。

コピーされた UIViewControllerTableViewの中には
'FILEBASENAME.m''FILEBASENAME.h'が格納されています。

'FILEBASENAME.h'を編集

まずは、FILEBASENAME.hを編集します。
今回はUITableViewを使うとき用のテンプレートファイルなので
UITableViewDataSourceとUITableViewDelegateを追加します。

___FILEBASENAME___.h
//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//

___IMPORTHEADER_cocoaTouchSubclass___

@interface ___FILEBASENAMEASIDENTIFIER___ : ___VARIABLE_cocoaTouchSubclass___
<
UITableViewDataSource,
UITableViewDelegate
>
@end

'FILEBASENAME.m'を編集

最後に、FILEBASENAME.mを編集します。

___FILEBASENAME___.m
//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//

#import "___FILEBASENAME___.h"

@interface ___FILEBASENAMEASIDENTIFIER___ ()

@end

@implementation ___FILEBASENAMEASIDENTIFIER___

- (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.
}

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

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 0;
}

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

    // Configure the cell...

    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

 // In a story board-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

これでテンプレートファイルの作成は完成です。

Xcodeで作成したテンプレートファイルを確認する

ファイルを新規追加すると、Cocoa Touchをコピーして作った My Cocoa Touch が追加されています。

Objective-C classを選択して、subclassをUIViewControllerにすると、 TableView というチェックボックスが増えています。

あとは、このチェックボックスにチェックを入れれば、テンプレートから作成されたファイルが作成されます。

参考にさせて頂いたshu223さんの記事Xcodeのファイルテンプレートを自作するでは
シングルトンクラスを作成するテンプレートの作り方が載っています。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10