忍者ブログ

[PR]

2024年04月24日
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

iPhone OS 3.0 におけるセルの変更点

2009年06月21日
3.0になったことによる、UITableView関連での変更点はUITableViewCellの変更だけのようです。これまではセルそのものがUILabelのようにtextを持っていたのですが、3.0以降ではセルは2種類のラベルを持ちます。textLabelとdetailTextLabelです。従って、3.0専用にアプリケーションを作る場合には、今までとは少しだけセルへのテキストの渡し方が変わります。これまで「セルの変数名.text」だったのが、「セルの変数名.textLabel.text」や「セルの変数名.detailTextLabel.text」になります。また、セルの作成時の宣言がフレームからスタイルに変更されています。スタイルは4種類の中から選びます。文章で書いていても分かりにくいので、スクリーンショットを掲載します。

cell3.PNG左にある画像が4種類のスタイルをそれぞれ適応したセルです。一番上がUITableViewCellStyleDefaultです。textLabelだけ表示される、今までに近い形の表示です。次がUITableViewCellStyleValue1です。タイトルと値を表示しますが、セルの幅を全て使う表示になります。その次がUITableViewCellStyleValue2です。タイトルと値が寄せて表示されます。最後がUITableViewCellStyleSubtitleです。2つのラベルが上下に並んで表示されます。ちなみに画像もこれまでは「セルの変数名.image」でしたが、「セルの変数名.imageView.image」となります。画像はスタイルがUITableViewCellStyleDefaultとUITableViewCellStyleSubtitleの場合だけラベルの左に挿入されます。


以下は上のスクリーンショットを撮影するのに使ったコードです。Navigation-based Applicationを選んでRootViewController.mの該当箇所をこのコードのように書き換えれば同じように表示されるはずです。Xcodeにそのままコピー&ペーストできるようにレイアウトはいじっていません。なお、これだけ少数のセルなので表示は全く問題ないはずですが、一応複数種のセルを使っていることもあるので、以前の記事『数種類のセルを使用してもスクロール速度の低下を極力減らす方法』と同じ書き方にしてあります。

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

    return 4;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    

// 行ごとに表示を変更します。

switch (indexPath.row) {

case 0: {

UITableViewCell *cellDefault = [tableView dequeueReusableCellWithIdentifier:@"cellDefault"];

if (cellDefault == nil) {

cellDefault = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellDefault"] autorelease];

}

cellDefault.textLabel.text = @"textLabel";

return cellDefault;

}

break;

case 1: {

UITableViewCell *cellValue1 = [tableView dequeueReusableCellWithIdentifier:@"cellValue1"];

if (cellValue1 == nil) {

cellValue1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellValue1"] autorelease];

}

cellValue1.textLabel.text = @"textLabel";

cellValue1.detailTextLabel.text = @"detailTextLabel";

return cellValue1;

}

break;

case 2: {

UITableViewCell *cellValue2 = [tableView dequeueReusableCellWithIdentifier:@"cellValue2"];

if (cellValue2 == nil) {

cellValue2 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"cellValue2"] autorelease];

}

cellValue2.textLabel.text = @"textLabel";

cellValue2.detailTextLabel.text = @"detailTextLabel";

return cellValue2;

}

break;

case 3: {

UITableViewCell *cellSubtitle = [tableView dequeueReusableCellWithIdentifier:@"cellSubtitle"];

if (cellSubtitle == nil) {

cellSubtitle = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellSubtitle"] autorelease];

}

cellSubtitle.textLabel.text = @"textLabel";

cellSubtitle.detailTextLabel.text = @"detailTextLabel";

return cellSubtitle;

}

break;

}

    UITableViewCell *cellDefault = [tableView dequeueReusableCellWithIdentifier:@"cellDefault"];

    if (cellDefault == nil) {

        cellDefault = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellDefault"] autorelease];

    }

return cellDefault;

}

 
2010年5月23日補足:
最後の実際に使われない部分はreturn nil;でもセル数との整合性が失われていなければ全く問題ないのですが、例えばtableView:numberOfRoawsInSection:で戻る値を4から5に変更するとここで空のオブジェクトが返り、結果としてアプリケーションが強制終了されてしまいます。そのような場合でも一応セルオブジェクトを返す事によりアプリケーション全体が落ちるのを避けられるようにしているため、このような一見すると面倒な表記になっています。
PR
Comment
  Vodafone絵文字 i-mode絵文字 Ezweb絵文字
Trackback
トラックバックURL: