0

カスタムスライダーのあるビューがあります。UITableViewCell のインスタンスを生成する TableViewController のサブクラスを使用しています。各 tableViewCell 内に、カスタム スライダー (UI ビュー) をサブビューとして追加します。ここには、スライダーのコントロール ノブとして機能するビューがあります。これは、ジェスチャー認識機能を備えた UIView の単なるサブクラスです。コントロール ノブ クラスの drawRect メソッドは、UIImage を受け取り、drawAtPoint を実行します。これはうまくいきます!要約すると、次のようになります。

UITableView -> UITableViewCell -> UITableViewCell.contentView -> SliderView -> SliderKnob -> UIImage

この問題は、テーブル ビューからテーブル セルをスクロールするときに発生します。ノブの UIImage は保持されます。セルがキューから取り出されるたびに、残りの画像の複製を取得することになります。いくつかの NSLog ステートメントをセットアップし、各サブビューで drawRect が呼び出されることを確認しました。セルのレンダリングを更新するために必要なことはありますか? UITableViewCell のサブビューで setNeedsDisplay を使用しようとしましたが、UIImage の複製を防ぐことができませんでした。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SliderView";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier] autorelease];
    }

    SliderView *slider = [[[SliderView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 130)] autorelease];

    // Configure the data for the cell.
    NSDictionary *dataItem = [data objectAtIndex:indexPath.row];
    slider.dimensionName = [dataItem objectForKey:@"Name"];
    slider.upperExtreme = [dataItem objectForKey:@"Upper Extreme"];
    slider.lowerExtreme = [dataItem objectForKey:@"Lower Extreme"];
    slider.score = [[dataItem objectForKey:@"Score"] intValue];

    cell.selectionStyle = UITableViewCellEditingStyleNone;
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"track_grainy.png"]];
    [cell.contentView addSubview:slider];

    return cell;
}
4

1 に答える 1

0

問題は、私たちが初心者のプログラマーであり、セルが更新されるたびに SliderView の新しいインスタンスを割り当てていたことです。これは明らかに非常に悪いことです。最終的に UITableViewCell をサブクラス化し、カスタム コンテンツ ビューをその init フレームでインスタンス化しました。これにより、カスタム ビューをメモリに保持し、セルを更新して更新することができます。その後、セルがテーブル ビューから解放されると、適切に解放されます。

于 2010-07-26T00:01:20.803 に答える