忍者ブログ

[PR]

2024年04月27日
×

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

テーブルのセルへの参照を2種類以上混在させる

2009年02月15日
アップルから提供されてるテーブル関連のサンプルを見ていると、cellForRowAtIndexPathで返すセルは必ずといっていいほど1種類に限定されています。そのため、セルを何種類も作っては駄目なのかと思いがちですが、別にそんな制限はありません。数種類のセルへの参照を作っておき、列ごとに返すセルを変える事もできます。ちょっと長いのですが一例として、Tiny3Dでオブジェクトやパートを編集するときに開くテーブルでのcellForRowAtIndexPathを示します。

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"]autorelease];

}

PositionTableViewCell *pCell = (PositionTableViewCell *)[tableView

dequeueReusableCellWithIdentifier:@"PositionTableViewCell"];

if (pCell == nil) {

pCell = [[[PositionTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"PositionTableViewCell"] autorelease];

}

SwitchTableViewCell *sCell = (SwitchTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchTableViewCell"];

if (sCell == nil) {

sCell = [[[SwitchTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchTableViewCell"] autorelease];

}
switch (indexPath.row) {
case 0: {

sCell.selectionStyle = UITableViewCellSelectionStyleNone;

sCell.myDictionary = self.myDictionary;

sCell.editedKey = @"display";

[sCell.switchCtl setOn:[[myDictionary objectForKey:@"display"] boolValue]
animated:NO];

sCell.nomalLabel.text = NSLocalizedString(@"Display",@"");

return sCell;

break;

}

case 1: {

cell.font = [UIFont systemFontOfSize:20];

cell.text = [NSString stringWithFormat:NSLocalizedString(@"Name: 
%@"@""),[myDictionary objectForKey:@"name"]];

return cell;

break;

}

case 2: {

pCell.enabledSetting = YES;

pCell.titleLabel.text = NSLocalizedString(@"Center",@"");

pCell.leftTitleLabel.text = @"X";

pCell.leftValueLabel.text = [NSString stringWithFormat:@"%f",
[[
myDictionary objectForKey:@"centerx"]floatValue]];

pCell.centerTitleLabel.text = @"Y";

pCell.centerValueLabel.text = [NSString stringWithFormat:@"%f",
[[
myDictionary objectForKey:@"centery"] floatValue]];

pCell.rightTitleLabel.text = @"Z";

pCell.rightValueLabel.text = [NSString stringWithFormat:@"%f",
[[myDictionary objectForKey:@"centerz"] floatValue]];

return pCell;

break;

}

case 3: {

pCell.enabledSetting = YES;

pCell.titleLabel.text = NSLocalizedString(@"Rotate",@"");

pCell.leftTitleLabel.text = @"X";

pCell.leftValueLabel.text = [NSString stringWithFormat:@"%f",
[[
myDictionary objectForKey:@"rotatex"] floatValue]];

pCell.centerTitleLabel.text = @"Y";

pCell.centerValueLabel.text = [NSString stringWithFormat:@"%f",
[[
myDictionary objectForKey:@"rotatey"] floatValue]];

pCell.rightTitleLabel.text = @"Z";

pCell.rightValueLabel.text = [NSString stringWithFormat:@"%f",
[[
myDictionary objectForKey:@"rotatez"] floatValue]];

return pCell;

break;

}

case 4: {

cell.font = [UIFont systemFontOfSize:20];

if ([myDictionary objectForKey:@"magnification"] == nil) {

[myDictionary setObject:[NSNumber numberWithFloat:1]
forKey:@"magnification"];

}

cell.text = [NSString stringWithFormatNSLocalizedString (
@"Magnification: %f",@""),[[myDictionary objectForKey:
@"magnification"]floatValue]];

return cell;

break;

}

}

    return cell;

}

 
こんな感じで、分岐後にreturnでさっさとセルへの参照を返してしまえば良いのですね。ただ、あまりたくさんのセル参照を用意すると、スクロール時のパフォーマンスが低下することも考えられますので注意が必要です。なお、一番最後のreturnはビルド時に警告が出るのを防ぐためのもので、実際にこれでセルを返す事はありません。






PR
Comment
  Vodafone絵文字 i-mode絵文字 Ezweb絵文字
Trackback
トラックバックURL: