2011-01-24 11 views
3

私はUITableViewを持っており、テーブルを表示するカスタムセルを作成しました。私は6つのUILablesを表示していますが、表示するレコードは20ですが、スクロールすると非常に遅いです。次のようになります:cellForRowAtIndexPath:すべての物事の私がここでやって(NSDateFormatter、CGMakeRect、floatToStringFormatていますので、スクロールが遅いがあることを実行するためにUITableViewカスタムセルが非常に遅い

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

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier"; 

    HistoryCell *cell = (HistoryCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HistoryCell" owner:nil options:nil]; 

     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[UITableViewCell class]]) 
       cell = (HistoryCell *) oneObject; 
    } 

    NSArray *object; 
    object = [cours objectForKey: [NSString stringWithFormat:@"%d", indexPath.section]]; 
    History *rowData = [object objectAtIndex:indexPath.row]; 

    if (rowData.month == 99) { 
     cell.hour.frame = CGRectMake(10, 0, 135, 35); 
     cell.data.hidden = YES; 
     cell.hour.textColor = [UIColor blackColor]; 
     cell.hour.font = [UIFont fontWithName:@"Verdana" size:17]; 
    } else { 
     cell.data.hidden = NO; 
     cell.hour.frame = CGRectMake(10, 16, 135, 19); 
     cell.hour.textColor = [UIColor grayColor]; 
     cell.hour.font = [UIFont fontWithName:@"Verdana" size:12]; 
    } 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"d (EEEE)"]; 
    [formatter setLocale:self.selectedLanguageLocale]; 
    NSString *stringFromDate = [formatter stringFromDate:rowData.data]; 
    [formatter release]; 

    cell.data.text = stringFromDate; 
    cell.hour.text = rowData.ora; 

    float Var1 = [rowData.Var2 floatValue]; 
    float Var2 = [rowData.Var2 floatValue]; 

    cell.R1.text = [self floatToStringFormat: [rowData.R1 floatValue]]; 
    cell.R2.text = [self floatToStringFormat: [rowData.R2 floatValue]]; 

    if (Var1 <= 0) { 
     cell.Var1.textColor = [UIColor greenColor]; 
    } else { 
     cell.Var1.textColor = [UIColor redColor]; 
    } 
    if (Var2 <= 0) { 
     cell.Var2.textColor = [UIColor greenColor]; 
    } else { 
     cell.Var2.textColor = [UIColor redColor]; 
    } 
    cell.Var1.text = [self floatToStringFormat:Var1]; 
    cell.Var2.text = [self floatToStringFormat:Var2]; 

    cell.selectionStyle = UITableViewCellSelectionStyleGray; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    return cell; 
} 

理由...のtableView -

は、これはどのように私です)、または細胞を再利用することに何か問題がありますか?

floatToStringFormat 4つの小数に番号をフォーマットする機能である:CustomCellIdentifierBanciHistoryCellIdentifier

- (NSString *)floatToStringFormat:(float)number{ 
    NSNumberFormatter *myFloat = [[NSNumberFormatter alloc] init]; 
    [myFloat setFormatterBehavior:NSNumberFormatterBehavior10_4]; 
    [myFloat setNumberStyle:NSNumberFormatterDecimalStyle]; 
    [myFloat setRoundingMode:NSNumberFormatterRoundHalfUp]; 
    [myFloat setMinimumFractionDigits:4]; 
    [myFloat setMaximumFractionDigits:4]; 
    NSString *res = [myFloat stringFromNumber:[NSNumber numberWithFloat:number]]; 
    [myFloat release]; 
    return res; 
} 

答えて

6

フォーマッタオブジェクトを作成して設定することは実際には高価な作業ですので、各関数呼び出しで同じであるため、フォーマッタオブジェクトを再利用することから始めます。だからあなたのデータソースクラスでそれらの静的変数またはインスタント変数にすると、以下の方法を作成、次のいずれか

//static variable case 
NSDateFormatter *formatter = nil; 
if (!formatter){ 
    formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"d (EEEE)"]; 
    [formatter setLocale:self.selectedLanguageLocale]; 
} 
NSString *stringFromDate = [formatter stringFromDate:rowData.data]; 
... 
3

まず、次の2つの異なる識別子を使用しています。

第2に、新しいセルが表示されるたびにNSArray *object;の後にすべてを行う必要がありますか?あなたがしない場合は、それをif (cell == nil) {ブロックに移動する必要があります。

+0

実際には本当ののはBanciHistoryCellIdentifierですが、ここで名前を変更してCustomCellIdentifierに改名して、わかりやすくなりました。この名前を変更することでエスケープしました。 – CristiC

2

私の経験から、3つ以上のサブビュー(デバイスとビューにも依存します)があると、テーブルビューの描画が大幅に遅くなります。直接drawRectのコンテンツを描画してください:サブビューを使用する代わりに、これは物事をスピードアップする必要があります。

-1

あなたはインターフェイスビルダーで設定CellIdentifierを持っていますか?コード内で使用しているものと正確に一致する必要があります。ペン先からセルをロードするブレークポイントを設定し、スクロール時にセルが再利用されていることを確認します。あなたはここで何をやっている

0

if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HistoryCell" owner:nil options:nil]; 

     for (id oneObject in nib) 
      if ([oneObject isKindOfClass:[UITableViewCell class]]) 
       cell = (HistoryCell *) oneObject; 
    } 

に行くには、適切にこれを行う方法についてdocumentationをお読みください。第2に、日付と数値を文字列に変換するのに時間がかかり過ぎる場合は、代わりに文字列値を格納し、それらを変更する必要があるときに値に変換します。