2015-10-16 4 views
0

EDIT: I have added a screenshot of the problemизготовленный под заказ коллекцию посмотреть ячейку

Я использовал пользовательскую коллекцию View Cell. Проблема в том, что когда я просматриваю список по горизонтали, я получаю 2 изображения в моем представлении изображения в некоторых ячейках. Пожалуйста, помогите мне понять, почему это происходит и как я могу его исправить?

Это вид коллекции code-

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    // static NSString * const reuseIdentifier = @"cell3"; 
    MostPopularViewCell *cell3 = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 



     cell3.clipsToBounds=YES; 
      cell3.mostproductprice.text=[[mostPopularArr objectAtIndex:indexPath.row] objectForKey:@"name"]; 
      cell3.mostproductnm.text=[[mostPopularArr objectAtIndex:indexPath.row] objectForKey:@"pirce"]; 


      NSDictionary *Scrapdict=[mostPopularArr objectAtIndex:indexPath.row]; 
        NSString *img_str=[Scrapdict objectForKey:@"small_image"]; 
      NSURL *url=[NSURL URLWithString:img_str]; 
      AsyncImageView *imageView = [[AsyncImageView alloc] initWithFrame:cell3.mostProductimg.bounds]; 
      imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 


      [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:imageView]; 
      //set image URL. AsyncImageView class will then dynamically load the image 
      ((AsyncImageView *)imageView).imageURL =url; 


      // imageView.clipsToBounds=YES; 
      imageView.userInteractionEnabled=YES; 
      imageView.contentMode = UIViewContentModeScaleAspectFill; 
      imageView.userInteractionEnabled=YES; 
      cell3.backgroundColor=[UIColor clearColor]; 
      cell3.mostProductimg.image=nil; 

        //imageView.layer.cornerRadius = 10; 
      imageView.backgroundColor=[UIColor clearColor]; 
      //cell3.mostProductimg.clipsToBounds=YES; 

      [cell3.mostProductimg addSubview:imageView]; 
      cell3.mostProductimg.userInteractionEnabled=YES; 
      [AsyncImageLoader defaultCache]; 

      [[AsyncImageLoader sharedLoader]loadImageWithURL:url target:nil success:nil failure:nil]; 

      // cell3.mostProductimg.clipsToBounds=YES; 

      cell3.mostPopularviewbtn.tag = indexPath.row; 
      [cell3.mostPopularviewbtn addTarget:self action:@selector(btnClick3:) forControlEvents:UIControlEventTouchUpInside]; 



    // Configure the cell 


    return cell3; 
} 

вид сбора клеток code-

- (void)awakeFromNib { 

} 
- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 

    if (self) { 
     // Initialization code 
     NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"MostPopularViewCell" owner:self options:nil]; 

     if ([arrayOfViews count] < 1) { 
      return nil; 
     } 

     if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) { 
      return nil; 
     } 

     self = [arrayOfViews objectAtIndex:0]; 




    } 

    return self; 

} 
+0

Можете ли вы предоставить скриншот того, что вы хотите и как оно выглядит сейчас? – Adeel

+0

Как подключить скриншот? –

+0

Редактировать свой вопрос и в текстовом режиме нажать «cntrl + g». Он откроет окно для выбора изображения из вашей системы. – Adeel

ответ

0

Перед [cell3.mostProductimg addSubview:imageView]; дать ваше изображение посмотреть тег и удалите подвид с тем же тегом из пользовательской ячейки. так что вы можете заменить [cell3.mostProductimg addSubview:imageView]; с

imageView.tag=123; 
UIView *viewToRemove = [cell3.mostProductimg viewWithTag:123]; 
if(viewToRemove !=nil) 
    [viewToRemove removeFromSuperview]; 
[cell3.mostProductimg addSubview:imageView]; 

Это должно работать. Проблема с вашим кодом заключается в том, что вы повторно используете ячейки и каждый раз добавляете imageView. Вы можете добавить imageView в xib пользовательской ячейки и создать для нее выход. После этого вы можете использовать выход для изменения своего изображения, а не добавлять новый образ вверху. Надеюсь, это поможет.

+0

thanx alot, это правильное решение. –