こんにちは。
最近ペースが落ち気味なのは重々承知ですが、なんとかがんばってます。
前回はプログラムをするだけして、結局画面にはなんも変化ないとこで終わってしまいました。
今日はいくぞ。
前は「CustomizedCellModel.m」まで終わったというところでした。
では「CustomizedCell.h」を修正します。
赤いとこが追加部分ね。
表示内容のデータを利用できるように、ヘッダファイルをインポートしてて、
名前と電話番号を表示するラベルを扱うメンバー変数を宣言してます。
で、最後に表示内容のデータをセットするメソッドを宣言してます。
#import < UIKit/UIKit.h>
#import "CustomizedCellModel.h"
@interface CustomizedCell : UITableViewCell {
UILabel *nameLabel;
UILabel *phoneLabel;
}
- (void)setModel:(CustomizedCellModel *)model;
@end
次は「CustomizedCell.m」です。
けっこうがっつり追加します。
ラベルの表示位置をサイズを定義しておいて、
initWithStyle~のメソッドでラベルの生成やら、初期化などを行ってます。
で、セルに表示内容オブジェクトをセットするためにsetModel:メソッドを実装してます。
#import "CustomizedCell.h"
@implementation CustomizedCell
#define NAME_X 90
#define NAME_Y 13
#define NAME_W 150
#define NAME_H 12
#define PHONE_X 90
#define PHONE_Y 29
#define PHONE_W 150
#define PHONE_H 20
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
// name
CGRect nameFrame = CGRectMake(NAME_X, NAME_Y, NAME_W, NAME_H);
nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
[nameLabel setFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]];
[nameLabel setTextColor:[UIColor grayColor]];
[self.contentView addSubview:nameLabel];
// phone
CGRect phoneFrame = CGRectMake(PHONE_X, PHONE_Y, PHONE_W, PHONE_H);
phoneLabel = [[UILabel alloc] initWithFrame:phoneFrame];
[phoneLabel setMinimumFontSize:[UIFont smallSystemFontSize]];
[phoneLabel setAdjustsFontSizeToFitWidth:TRUE];
[self.contentView addSubview:phoneLabel];
}
return self;
}
~(中略)~
- (void)setModel:(CustomizedCellModel *)model {
if (model == nil) {
return;
}
self.accessoryType = [model hasDisclosure]
? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
[nameLabel setText:[model name]];
[phoneLabel setText:[model phone]];
}
@end
最後に「ContactListViewController.m」です。
これはデフォルトのセルの代わりにカスタマイズしたセルでテーブルが表示される様に修正してます。
まず、CustomizedCellクラスのヘッダファイルをインポート。
表示内容にあわせてセルの高さをあわせるために、tableView:
heightForRowAtIndexPath:メソッドを実装します。
カスタマイズしたセルを使える様にtableView:cellForRowAtIndexPath:の中身を修正します。
以前のはコメントとして、ここでは赤文字で追加コードとして載せてます。
#import "ContactListViewController.h"
#import "CustomizedCell.h"
~(中略)~
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomizedCell";
CustomizedCell *cell = (CustomizedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomizedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
CustomizedCellModel *model
= [[CustomizedCellModel alloc] initWithPersonalData:[
manager loadPersonalDataAtIndex:indexPath.row]
hasDisclosure:FALSE];
[cell setModel:model];
return cell;
/*
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.textLabel.text = [[manager loadPersonalDataAtIndex:indexPath.row] name];
// [cell setTextLabel:(UILabel *):[[manager loadPersonalDataAtIndex:indexPath.row] name]];
return cell;
*/
}
ここまでできたら、一通り完成です。。
ちゃんとうごくかな?


名前と一緒に電話番号が表示されました!
いやーめでたしめでたし。。
でもこれで、前やろうとしていたセルの編集とか削除とかを始められるということなんで、
まだまだ、これからなんです。。。。

にほんブログ村
> 次のテーマ テーブルをつくっていこう。
> テーブルのデータを管理するクラスを作ってみる
> テーブルビューのコントローラーを作ってみる。
> ついにテーブルの表示完成!
> テーブルをいじくる。
> テーブルを本当にいじくる
> テーブルを本当に実装を始めるよ。
> テーブルを改造しましょ。
> テーブル改造のつづきやけど。。
> テーブルのセルに電話番号を追加してみる。(★この記事★)
> モード切り替え用ボタンの追加
> セル(データ)の削除処理と並び替え!
> テーブルいじくりも大詰め?
最近ペースが落ち気味なのは重々承知ですが、なんとかがんばってます。
前回はプログラムをするだけして、結局画面にはなんも変化ないとこで終わってしまいました。
今日はいくぞ。
前は「CustomizedCellModel.m」まで終わったというところでした。
では「CustomizedCell.h」を修正します。
赤いとこが追加部分ね。
表示内容のデータを利用できるように、ヘッダファイルをインポートしてて、
名前と電話番号を表示するラベルを扱うメンバー変数を宣言してます。
で、最後に表示内容のデータをセットするメソッドを宣言してます。
#import < UIKit/UIKit.h>
#import "CustomizedCellModel.h"
@interface CustomizedCell : UITableViewCell {
UILabel *nameLabel;
UILabel *phoneLabel;
}
- (void)setModel:(CustomizedCellModel *)model;
@end
次は「CustomizedCell.m」です。
けっこうがっつり追加します。
ラベルの表示位置をサイズを定義しておいて、
initWithStyle~のメソッドでラベルの生成やら、初期化などを行ってます。
で、セルに表示内容オブジェクトをセットするためにsetModel:メソッドを実装してます。
#import "CustomizedCell.h"
@implementation CustomizedCell
#define NAME_X 90
#define NAME_Y 13
#define NAME_W 150
#define NAME_H 12
#define PHONE_X 90
#define PHONE_Y 29
#define PHONE_W 150
#define PHONE_H 20
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
// name
CGRect nameFrame = CGRectMake(NAME_X, NAME_Y, NAME_W, NAME_H);
nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
[nameLabel setFont:[UIFont systemFontOfSize:[UIFont smallSystemFontSize]]];
[nameLabel setTextColor:[UIColor grayColor]];
[self.contentView addSubview:nameLabel];
// phone
CGRect phoneFrame = CGRectMake(PHONE_X, PHONE_Y, PHONE_W, PHONE_H);
phoneLabel = [[UILabel alloc] initWithFrame:phoneFrame];
[phoneLabel setMinimumFontSize:[UIFont smallSystemFontSize]];
[phoneLabel setAdjustsFontSizeToFitWidth:TRUE];
[self.contentView addSubview:phoneLabel];
}
return self;
}
~(中略)~
- (void)setModel:(CustomizedCellModel *)model {
if (model == nil) {
return;
}
self.accessoryType = [model hasDisclosure]
? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
[nameLabel setText:[model name]];
[phoneLabel setText:[model phone]];
}
@end
最後に「ContactListViewController.m」です。
これはデフォルトのセルの代わりにカスタマイズしたセルでテーブルが表示される様に修正してます。
まず、CustomizedCellクラスのヘッダファイルをインポート。
表示内容にあわせてセルの高さをあわせるために、tableView:
heightForRowAtIndexPath:メソッドを実装します。
カスタマイズしたセルを使える様にtableView:cellForRowAtIndexPath:の中身を修正します。
以前のはコメントとして、ここでは赤文字で追加コードとして載せてます。
#import "ContactListViewController.h"
#import "CustomizedCell.h"
~(中略)~
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomizedCell";
CustomizedCell *cell = (CustomizedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomizedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
CustomizedCellModel *model
= [[CustomizedCellModel alloc] initWithPersonalData:[
manager loadPersonalDataAtIndex:indexPath.row]
hasDisclosure:FALSE];
[cell setModel:model];
return cell;
/*
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.textLabel.text = [[manager loadPersonalDataAtIndex:indexPath.row] name];
// [cell setTextLabel:(UILabel *):[[manager loadPersonalDataAtIndex:indexPath.row] name]];
return cell;
*/
}
ここまでできたら、一通り完成です。。
ちゃんとうごくかな?


名前と一緒に電話番号が表示されました!
いやーめでたしめでたし。。
でもこれで、前やろうとしていたセルの編集とか削除とかを始められるということなんで、
まだまだ、これからなんです。。。。
にほんブログ村
> 次のテーマ テーブルをつくっていこう。
> テーブルのデータを管理するクラスを作ってみる
> テーブルビューのコントローラーを作ってみる。
> ついにテーブルの表示完成!
> テーブルをいじくる。
> テーブルを本当にいじくる
> テーブルを本当に実装を始めるよ。
> テーブルを改造しましょ。
> テーブル改造のつづきやけど。。
> テーブルのセルに電話番号を追加してみる。(★この記事★)
> モード切り替え用ボタンの追加
> セル(データ)の削除処理と並び替え!
> テーブルいじくりも大詰め?



