2011-06-27 10 views
1

私はカスタムビューを追加したいと思っています。現在のところ、細胞が再利用されると、バックグラウンドイメージが壊れてしまい、トップセルがボトムセルイメージなどを使用するようです。UITableViewCell backgroundViewが再利用されるべきでないときに再利用される

この場合、セルを正しく再使用していますか?

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

    static NSString *CellIdentifier = @"Cell"; 

    UIImageView *linkAvailableImageView = nil; 
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)]; 
    UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)]; 

    UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)]; 
    UIImageView *selectedBackgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, tableView.bounds.size.width-20, 44)]; 


    // Asset 
    Asset *asset = nil; 
    asset = (Asset *)[items objectAtIndex:indexPath.row]; 

    int count = [items count]; 

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

     if (indexPath.row == 0 && count > 1) { 
      backgroundImage.frame = CGRectMake(10, 0, tableView.bounds.size.width-20, 45); 
      backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10]; 
      selectedBackgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45); 
      selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedTop.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10]; 
     } else if (indexPath.row == count-1 && count > 1) { 
      backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10]; 
      selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedBottom.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10]; 
     } else if (indexPath.row == 0 && count == 1) { 
      backgroundImage.frame = CGRectMake(10, -1, tableView.bounds.size.width-20, 45); 
      backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10]; 
      selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedSingle.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:10]; 
     } else { 
      backgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10]; 
      selectedBackgroundImage.image = [[UIImage imageNamed:@"MDACCellBackgroundSelectedMiddle.png"] stretchableImageWithLeftCapWidth:1 topCapHeight:10]; 
     }//end 

     backgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     [backgroundView addSubview:backgroundImage]; 
     [backgroundImage release]; 

     selectedBackgroundImage.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
     [selectedBackgroundView addSubview:selectedBackgroundImage]; 
     [selectedBackgroundImage release]; 

     cell.backgroundView = backgroundView; 
     [backgroundView release]; 

     cell.selectedBackgroundView = selectedBackgroundView; 
     [selectedBackgroundView release]; 

     linkAvailableImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.bounds.size.width-39, 9, 24, 24)] autorelease]; 
     linkAvailableImageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     linkAvailableImageView.image = [UIImage imageNamed:@"MDACLinkArrow.png"]; 
     linkAvailableImageView.tag = 3; 
     [cell.contentView addSubview:linkAvailableImageView]; 

    } else { 
     linkAvailableImageView = (UIImageView *)[cell.contentView viewWithTag:3]; 
    } 

    // Get asset 
    cell.textLabel.opaque = NO; 
    cell.textLabel.text = asset.name; 
    cell.textLabel.font = [UIFont boldSystemFontOfSize:17]; 
    cell.textLabel.backgroundColor = [UIColor colorWithWhite:94./255. alpha:1]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.textLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.6]; 
    cell.textLabel.shadowOffset = CGSizeMake(0, -1); 

    // Set the kind of disclosure indicator 
    if ([asset.children intValue] > 0) { 
     //cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    }//end 

    // Lazy Load the image 
    if (!asset.appIcon) { 

     // Download icon 
     [self startIconDownload:asset forIndexPath:indexPath]; 

     // if a download is deferred or in progress, return a placeholder image 
     cell.imageView.image = [UIImage imageNamed:@"default-icon.png"]; 

    } else { 
     cell.imageView.image = asset.appIcon; 
    }//end 

    return cell; 

}//end 

答えて

0

ここでの問題は、テーブルビューの位置に関係なく、同じセル識別子を使用していることです。

したがって、最初にindexPath.rowとcountに基づいてセルを作成しますが、それらのセルを@ "Cell"の識別子に関連付けます。したがって、dequeueReusableCellWithIdentifierをスクロールすると、リストの先頭に設定されたセル(indexPath.row == 0 & &> 1)が返され、リストの末尾に使用されます。

あなたは、あなただけが作成されているテーブル内の位置のために設定されている細胞を再利用するように、セル識別子がブロックであれば、あなたの細胞==のnillの先頭にコードを反映していることを確認する必要があります。

Eikoと指摘すると、UIViewとUIImageViewオブジェクトも漏れています。 ifブロックに貼り付けたり、明示的に解放したり、autoreleaseにすることができます。

+0

ありがとう、これは私が探していた答えです。 –

+0

@ nic-hubbard問題はありません:)うれしいです。 – RedBlueThing

0

申し訳ありませんが、そのコードは、多くの問題があります:あなたはUIViewUIImageViewオブジェクトをリークしている、と細胞の全体の再利用は、したがって、あなたの問題間違っています。

if (cell == nil)部分に新しいセル(ビュー付き)を設定する必要があります。release/autoreleaseビューを忘れないでください。次に、そのブロックの外側で、セルを適切に設定します(その内容を設定します)。

アップルのサンプルプロジェクトの一部を見ることを強くお勧めします。

+0

これは、オープンソースのサンプルプロジェクトからコピーされたコードです。私はbackgroundViewsの使い方を感じて、セル再利用のこの問題に遭遇しました。どのAppleの例が役に立ちますか? –

+0

UITableViewのドキュメントを検索する - いくつかのサンプルプロジェクトへのリンクがあります。このコードは動作しません。 :) – Eiko

関連する問題