tableViewCellの複製無効化

| コメント(0) | トラックバック(0)

UITableViewのCellは、メモリ効率よく管理するためにスクロールして完全に消えると、次に出現するセルに対して、その消えたセルが再利用される。その結果、「tableView.imageView.image」でユニークな画像を定義していると、それがそのまま使用され、不都合な場合がある。

以下、対処法。要は、セルの複製を無効にしている。大量のセルを使用する場合は、別の方法を検討したほうがいい。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    static NSString *CellIdentifier = @"Cell";
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    //}
    #pragma unused(cell)
    // Configure the cell...
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

「#pragma unused(cell)」を入れないと、Build And Analyze でエラーがでる。

トラックバック(0)

トラックバックURL: http://www.ayabin.jp/mt5/mt-tb.cgi/94

コメントする

このブログ記事について

このページは、webmasterが2011年3月26日 14:43に書いたブログ記事です。

ひとつ前のブログ記事は「didSelectedの無効化」です。

次のブログ記事は「シミュレータ」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。