LoginSignup
1
2

More than 5 years have passed since last update.

[古い方法]Master-Detail ApplicationでInterface Builderを使ってUITableViewCellのカスタマイズ

Last updated at Posted at 2012-02-14

GUIでの作業

  • ビューの追加
    1. NewFileからUIViewControllerのサブクラスを新規に作成。名称はPhotoCellControllerのようにする
    2. 生成されたxibファイルを開き、UIViewを消し、Objects一覧からテーブルセル単体をD&Dする
    3. セル上に必要な画面構成要素(UIImageViewやUILabel、UIButtonなど)を好きに配置する
    4. ↓の手順に従い、コードを変更する
    5. アシスタントエディタを開き、PhotoCell内の要素をPhotoCellクラスにアウトレット接続する。PhotoCell自体をPhotoCellControllerにアウトレット接続する。

コードの変更作業

  • 上記4.の手順
    1. PhotoCellController.hで、PhotoCellControllerの上にPhotoCellクラスを書く
    2. PhotoCellController.mで、PhotoCellControllerの上にPhotoCellクラスを実装する
PhotoCellController.h
@interface PhotoCell : UITableViewCell {
@private
}
@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@end

@interface PhotoCellController : UIViewController
{
@private IBOutlet PhotoCell* _cell;
}
@end
PhotoCellController.m
#import "PhotoCellController.h"
@implementation PhotoCell
@synthesize imageView;
@synthesize nameLabel;
- (void)dealloc
{
    self.imageView = nil;
    self.nameLabel = nil;
}
@end

@implementation PhotoCellController
〜〜……
  • MasterViewController.mでカスタムセルを使う部分の変更
    1. #import "PhotoCellController.h"し、UITableViewCellの部分を以下のように変更する
MasterViewController.m
@interface MasterViewController ()
- (void)configureCell:(PhotoCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end

// Customize the appearance of table view cells.
- (PhotoCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PhotoCell";
    PhotoCell *cell = (PhotoCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        PhotoCellController* controller = [[PhotoCellController alloc] initWithNibName:@"PhotoCellController" bundle:nil];
        cell = (PhotoCell *)controller.view;
    }
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(PhotoCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
〜〜
}
1
2
2

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
1
2