UITableViewCellカスタマイズ

| コメント(0) | トラックバック(0)

http://d.hatena.ne.jp/KishikawaKatsumi/20081024/1224857278を参考にカスタマイズ法を整理する。

新規プロジェクトでテンプレート「Navigation-Based Application」を選択。プロジェクト名を「TableCell1」として作成。

プロジェクトが作られたら早速、新規ファイルの追加で、iOSのUser Interfaceから「View xib」を選択。ファイル名は「FeedListCell.xib」とする。

0.png

 プロジェクトに追加された「FeedListCell.xib」をダブルクリックして立ち上げ(Interface Builder)、元からあるViewを削除する。

2.png

↓こんな感じ

2-1.png

Interface BuiderのLibraryからTable View Cellをドラッグして配置。

2-2.png

配置したTable View Cellをダブルクリックして、Viewを立ち上げ

3.png

適当にデザインする。とりあえず、UILabelを2つ配置。

3-1.png

配置すると以下のようになる。

3-2.png

AttibutesのIdentifierに「FeedListCell」と入力して保存。

4-2.png

続いてXcodeで対応するUITableViewCellを継承するクラスとViewControllerを作る。
まずは、UITableViewCell。新規ファイル追加でiOSのCocoa Touch ClassからObjective-C classを選択、Subclass ofはUITableViewCellを指定。ファイル名は「FeedListCell」とする。

4-1.png

作成した「FeedListCell.h」「FeedListCell.m」にそれぞれ以下のように記述。

「FeedListCell.h」

#import <UIKit/UIKit.h>

@interface FeedListCell : UITableViewCell {
    IBOutlet UILabel *player;
    IBOutlet UILabel *point;
}
@property (nonatomic, retain) UILabel *player;
@property (nonatomic, retain) UILabel *point;
@end

「FeedListCell.m」

#import "FeedListCell.h"

@implementation FeedListCell

@synthesize player,point;

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc {
    [player release];
    [point release];
    [super dealloc];
}

@end
 

次に「追加>新規ファイル」でiOSのCocoa Touch ClassからUIViewController subclassを選択。「UITableViewController subclass」のチェックは外す。ファイル名は「FeedListCellController」とする。

 

5-1.png

生成された「FeedListCellController.h」に下のコードを記述。

「FeedListCellController.h」

#import <UIKit/UIKit.h>
#import "FeedListCell.h"

@interface FeedListCellController : UIViewController {
    IBOutlet FeedListCell *cell;
}
@property (nonatomic, retain) FeedListCell *cell;
@end
 

 「FeedListCell.xib」を立ち上げ、「File's Owner」を選択してIdentityのClassを「FeedListCellController」に、続いて「Feed List Cell」を選択してIdentityのClassを「FeedListCell」に変更する。

5-3.png

5-2.png

変更すると以下のようになる。

5-4.png

 そのままFile's Ownerを右クリックして、cellとviewをそれぞれ「Feed List Cell」と接続する。

6-1.png

次にFeed List Cell(FeedListCell)を右クリックして、playerとpointを以下のように接続する。

6-2.png

これでセルの準備は完了。

カスタマイズしたセルを実際に表示には、「RootVIewController.m」に以下のように記述。

#import "RootViewController.h"
#import "FeedListCellController.h"

@implementation RootViewController

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"FeedListCell";
   
    FeedListCell *cell =(FeedListCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        FeedListCellController *controller=[[FeedListCellController alloc] initWithNibName:CellIdentifier bundle:nil];
        cell=(FeedListCell*)controller.view;
        [controller release];
    }
   
    // Configure the cell.
    [cell.player setText:@"ayabin"];
    [cell.point setText:@"50"];


    return cell;
}
 

ビルドした結果。

7.png

トラックバック(0)

トラックバックURL: http://www.ayabin.jp/mt5/mt-tb.cgi/86

コメントする

このブログ記事について

このページは、webmasterが2010年12月 6日 11:26に書いたブログ記事です。

ひとつ前のブログ記事は「tableView」です。

次のブログ記事は「ManagedObjectの属性取得」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。