UItableviewCell单元重用问题

时间:2016-12-16 07:43:23

标签: ios objective-c uitableview uiscrollview

我在scrollview中水平UItableviewCell显示图片。

问题是当我滚动tableview图像不匹配时。我的代码是我的参考:

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

ManufacturersTableviewCustomCell * cell = (ManufacturersTableviewCustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell == nil)
{
    cell = [[ManufacturersTableviewCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}


cell.scrollview.delegate = self;
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [UIView new] ;
cell.selectedBackgroundView = [UIView new] ;
cell.scrollview.tag = indexPath.row;
cell.btnViewAll.tag = indexPath.row;

NSDictionary *dict ;
NSArray *aryImages;


if(isFiltered && [aryFilteredProducts count]>0){
    dict = [[aryFilteredProducts objectAtIndex:indexPath.row]valueForKey:@"manufacturers"];
    aryImages = [[aryFilteredProducts objectAtIndex:indexPath.row]valueForKey:@"sub_categories"];
}
else if([aryProducts count]>0){
    dict = [[aryProducts objectAtIndex:indexPath.row]valueForKey:@"manufacturers"];
    aryImages = [[aryProducts objectAtIndex:indexPath.row]valueForKey:@"sub_categories"];

}

NSString *strUppercase = [[dict valueForKey:@"manufacturers_name"] uppercaseString];
cell.lblManufacturerName.text = strUppercase;

 NSString *strImgUrl = [NSString stringWithFormat:@"%@/%@",KServerUrl,[dict valueForKey:@"original_image"]];

if(![[NSURL URLWithString: strImgUrl] isKindOfClass:[NSNull class]])
    [cell.imgViewManufacturer setImageWithURL:[NSURL URLWithString:strImgUrl] placeholderImage:[UIImage imageNamed:@"Background.png"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

[cell.viewBorder.layer setBorderColor: [[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0] CGColor]];
[cell.viewBorder.layer setBorderWidth: 1.0];

[cell.imgViewManufacturer.layer setBorderColor: [[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0] CGColor]];
[cell.imgViewManufacturer.layer setBorderWidth: 1.0];

CGFloat contentWidth = 0;

for(int i=0;i< [aryImages count];i++)
{
    if(SCREEN_MAX_LENGTH == 568){
        if([aryImages count] >3){
            cell.btnViewAll.hidden = NO;
        }
        else  {
            cell.btnViewAll.hidden = YES;
        }
    }
    else{
        if([aryImages count] >5){
            cell.btnViewAll.hidden = NO;
        }
        else  {
            cell.btnViewAll.hidden = YES;
        }
    }
    NSDictionary *dictImageData = [aryImages objectAtIndex:i];

    UIImageView *imgView   = [[UIImageView alloc]initWithFrame:CGRectMake([aryImages indexOfObject:dictImageData]*(80 + 5),0,80,80)];

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
        [imgView setFrame:CGRectMake([aryImages indexOfObject:dictImageData]*(130 + 5),0,130,130)];
    }
     [imgView.layer setBorderColor: [[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0] CGColor]];
     [imgView.layer setBorderWidth: 1.0];
    imgView.tag  = i;
    // add tapgesture

    UITapGestureRecognizer *tapgestureImg = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageTapped:)];
    [imgView addGestureRecognizer:tapgestureImg];

    NSString *strImgUrlSubCat = [NSString stringWithFormat:@"%@/%@",KServerUrl,[dictImageData valueForKey:@"sub_product_img"]];

    imgView.userInteractionEnabled = NO;

    imgView.image = [UIImage imageNamed:@"Background.png"];

    if(![[NSURL URLWithString: strImgUrlSubCat] isKindOfClass:[NSNull class]]){
        [imgView setImageWithURL:[NSURL URLWithString:strImgUrlSubCat ] placeholderImage:[UIImage imageNamed:@"Background.png"] usingActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        imgView.userInteractionEnabled = YES;

    }
    [cell.scrollview addSubview:imgView];

    UIView *vwBackGround = [[UIView alloc]initWithFrame:CGRectMake([aryImages indexOfObject:dictImageData]*(80 + 5), 60, 80, 20)];
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
        [vwBackGround setFrame:CGRectMake([aryImages indexOfObject:dictImageData]*(130 + 5), 90, 130, 40)];
    }
    vwBackGround.backgroundColor = [UIColor blackColor];
    vwBackGround.alpha = 0.5;
    [cell.scrollview  addSubview:vwBackGround];

    UILabel *lblProductName = [[UILabel alloc]initWithFrame:CGRectMake([aryImages indexOfObject:dictImageData]*(80 + 5)+4, 60, 70, 20)];
    lblProductName.font = [UIFont fontWithName:@"Helvetica" size:10.0];

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
        [lblProductName setFrame:CGRectMake([aryImages indexOfObject:dictImageData]*(130 + 5)+4, 90, 120, 40)];
        lblProductName.font = [UIFont fontWithName:@"Helvetica" size:17.0];

    }
    lblProductName.textColor = [UIColor whiteColor];
    lblProductName.text  = [dictImageData valueForKey:@"sub_product_name"];
    [cell.scrollview addSubview:lblProductName];

    [cell.scrollview bringSubviewToFront:vwBackGround];
    [cell.scrollview bringSubviewToFront:lblProductName];

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
        contentWidth += 130 + ([aryImages indexOfObject:dictImageData]==0?0:5);
    }
    else{
        contentWidth += 80 + ([aryImages indexOfObject:dictImageData]==0?0:5);

    }
}


cell.scrollview.contentSize = CGSizeMake(contentWidth, cell.scrollview.frame.size.height);

return cell;
}

您能否建议如何解决此问题。

1 个答案:

答案 0 :(得分:0)

在添加之前删除图像视图..

通过在cellforrowatindexpath中添加以下代码来解决它

   if ([cell.scrollview subviews]){
    for (UIView *subview in [cell.scrollview subviews]) {
        if([subview isKindOfClass:[UIImageView class]] || [subview isKindOfClass:[UIView class]]){
        [subview removeFromSuperview];
        }
    }
}