忍者ブログ

[PR]

2024年03月29日
×

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

数種類のセルを使用してもスクロール速度の低下を極力減らす方法

2009年05月19日
以前の記事『テーブルのセルへの参照を2書類以上混在させる』で数種類のセルを混在させる方法を書きましたが、あの方法には実は大きな欠点がありました。その欠点はテーブルが画面に納まりきる場合には全く問題にならないのですが、テーブルのセル数が多くスクロールさせる必要がある場合に大問題になります。そう、スクロール速度がとても遅くなるのです。

最近Tiny3Dで各基本図形の詳細設定をする画面のテーブルスクロールを少しでも高速にしようといろいろ試行錯誤していてわかったのですが、セルの再利用を行えるかの判定部分の処理はかなり重たいようです。スクロール時には画面に表示するセルが次々変化するため、その度に行われるセル再利用判定がスクロールの邪魔をしてしまっていたのです。

つまり、それらの判定を必要最小限に行うようにした方がテーブルのスクロールを高速に出来ます。そのためには、たとえ複数回の重複部分が発生してコードの行数が多くなろうと、セルのセクションや行での分岐後で、そのとき必要なセルに限定した再利用判定をした方が良いということになります。以前のコードをテーブルのスクロール速度も考慮したものに変更すると、以下のように書き換えられます。

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

switch (indexPath.row) {

case 0: {

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

if (sCell == nil) {

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

}

sCell.selectionStyle = UITableViewCellSelectionStyleNone;

sCell.myDictionary = self.myDictionary;

sCell.editedKey = @"display";

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

 

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

return sCell;

break;

}

case 1: {

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

if (cell == nil) {

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

}

cell.font = [UIFont systemFontOfSize:20];

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

return cell;

break;

}

case 2: {

HorizontalPositionTableViewCell *pCell =
  (HorizontalPositionTableViewCell *)[tableView
 
dequeueReusableCellWithIdentifier:@"HorizontalPositionTableViewCell"];

if (pCell == nil) {

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

}

pCell.enabledSetting = YES;

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

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

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

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

return pCell;

break;

}

case 3: {

HorizontalPositionTableViewCell *pCell = 
(HorizontalPositionTableViewCell *)[tableView
 
dequeueReusableCellWithIdentifier:@"HorizontalPositionTableViewCell"];

if (pCell == nil) {

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

}

pCell.enabledSetting = YES;

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

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

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

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

return pCell;

break;

}

case 4: {

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

if (cell == nil) {

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

}

cell.font = [UIFont systemFontOfSize:20];

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

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

}

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

return cell;

break;

}

}

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

    if (cell == nil) {

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

    }

    return cell;

}

 
なんだかぱっと見では複数回同じコードが見えていて簡潔でなくなったように見えます。しかし実機を用いてスクロールさせた場合の速度は、このように記述しておいた方が明らかに速くなります。なお前回も書きましたが、最後の部分は標準設定でビルドしたときの警告回避用なので、実際使われる事はありません。
PR
Comment
  Vodafone絵文字 i-mode絵文字 Ezweb絵文字
Trackback
トラックバックURL: