2012-04-15 10 views
0

次のコードを使用して、インデックスパスのすべての行にセルを実装しました: 問題は、tableViewをスクロールすると、セルがUIImageView * itemimageview 1つのセルの1つのセルで使用しようとしました。iOS TableView再利用でセルに多くのサブビューがロードされる

for (UIImageView *sView in cell.subviews) { 
     [sView removeFromSuperview]; 
    } 

ただし、1つのセルのすべてのサブビューが削除されます。この問題を解決する方法...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = [indexPath row]; 
    NSUInteger oldRow = [lastIndexPath row]; 
    static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier"; 

    //dequeueReusableCellWithIdentifier -- 
    // Returns a reusable table-view cell object located by its identifier. 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifier] autorelease]; 

    } 
    /* 
    for (UIImageView *sView in cell.subviews) { 
     [sView removeFromSuperview]; 
    } 
    */ 
    UIImageView *itemimageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 232, 54)]; 
    itemimageview.image=[UIImage imageNamed:[tabsImageArray objectAtIndex:row]]; 

    itemimageview.userInteractionEnabled = YES; 
    [cell.contentView addSubview:itemimageview]; 
    [itemimageview release]; 

    UIImageView *dictIcon=[[UIImageView alloc]initWithFrame:CGRectMake(30, 18, 30, 30)]; 
    dictIcon.image=[UIImage imageNamed:@"dictionary_icon.png"]; 
    dictIcon.userInteractionEnabled = YES; 
    [cell.contentView addSubview:dictIcon]; 
    [dictIcon release]; 

    UILabel *dictNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 23, 100, 21)]; 
    dictNameLabel.text = dictName; 
    dictNameLabel.textColor = [UIColor whiteColor]; 
    dictNameLabel.shadowColor = [UIColor blackColor]; 
    dictNameLabel.backgroundColor = [UIColor clearColor]; 
    dictNameLabel.userInteractionEnabled = YES; 
    [cell.contentView addSubview:dictNameLabel]; 
    [dictNameLabel release]; 


    //cell.textLabel.text = [tabsImageArray objectAtIndex:row]; 
    cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone; 

    return cell; 
} 

答えて

0

が通って、チェックを実行している考えてみましょう?[ビューisKindOfClass:[UIImageViewクラス]ビューが削除するビューの右のタイプであるかどうかをチェックします。

また、UIViewタグのプロパティでビューにタグを付けることで、サブビューを一度追加できるようになり、再利用で再作成する必要がなくなります。ここで

は、あなたがこれを行う方法は以下のようになります

#define ImageViewOneTag 1001 
#define ImageViewTwoTag 1002 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path { 
    static NSString *CellID = @"CellID"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID]; 

    UIImageView *imageViewOne = nil; 
    UIImageView *imageViewTwo = nil; 

    if (cell) { 
     // You've caught a reusable cell. Fetch the image views by their tag. 

     imageViewOne = (UIImageView *)[cell viewWithTag:ImageViewOneTag]; 
     imageViewTwo = (UIImageView *)[cell viewWithTag:ImageViewTwoTag]; 
    } else { 
     // You haven't got a reusable cell. Make one, and make and add the image views to the contentView. 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID]; 

     imageViewOne = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)]; 
     [imageViewOne setTag:ImageViewOneTag]; 

     imageViewTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 20.0, 20.0, 20.0)]; 
     [imageViewTwo setTag:ImageViewTwoTag]; 

     UIView *contentView = cell.contentView; 
     [contentView addSubview:imageViewOne]; 
     [contentView addSubview:imageViewTwo]; 
    } 

    // By this stage, you've either retrieved a reusable cell, or you've made a new one. Either way, imageViewOne and imageViewTwo now have a reference to the views you mean. 
    imageViewOne.image = *imageOneForRow*; 
    imageViewTwo.image = *imageTwoForRow*; 

    return cell; 
} 
+0

ビューにタグを付ける方法がわかりません。これを行う方法を教えてください。私はtableView開発のために本当に新鮮です。 – Samblg

+0

問題ありません。主な答えを編集します。 – thebarcodeproject

0

私はただのUITableViewCellを継承ココアタッチクラスを作成し、カスタムテーブルビューのセルを使用してお勧めします。これが助けてくれることを願って!

関連する問題