0

我在我的collectionView中有双重项目。我该如何解决它?UICollectionView双重项目

我有UITableViewCell其中UICollectionViewCell里面。

我根据它的数量设置了元素的大小和方向。

enter image description here

我的手机

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     self.layout = [[UICollectionViewFlowLayout alloc] init]; 
     self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); 
     self.layout.itemSize = CGSizeMake(70, 70); 
     self.layout.scrollDirection = UICollectionViewScrollDirectionVertical; 
     self.collectionView = [[IndexedCollectionView alloc] initWithFrame:CGRectMake(10, 250, 60, 320) collectionViewLayout:self.layout]; 
     [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier]; 
     self.collectionView.backgroundColor = [UIColor whiteColor]; 
     self.collectionView.showsHorizontalScrollIndicator = NO; 
     [self.contentView addSubview:self.collectionView]; 

    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 


-(void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    self.collectionView.frame = self.contentView.bounds; 
} 

-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index 
{ 
    self.collectionView.dataSource = dataSourceDelegate; 
    self.collectionView.delegate = dataSourceDelegate; 
    self.collectionView.index = index; 

    [self.collectionView reloadData]; 
} 

委托方法

-(FeedCell *)collectionView:(IndexedCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    FeedCell *cell = (FeedCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath]; 

    UIImageView *imageView = [[UIImageView alloc]init]; 
    imageView.frame = collectionView.frame; 
    imageView.contentMode = UIViewContentModeScaleAspectFit; 
    imageView.clipsToBounds = YES; 

    NSInteger count = [self test:collectionView].count; 
    if (count == 0) 
    { 
     return nil; 
    } 

    NSInteger imageIndex = 0; 

    if (count == 1) 
    { 
     [imageView setFrame:CGRectMake(0, 110, 310, 200)]; 
     imageIndex = indexPath.item; 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 

    } 
    if (count == 2) 
    { 
     [imageView setFrame:CGRectMake(0, 110, 150, 200)]; 
     imageIndex = indexPath.item; 
     imageView.contentMode = UIViewContentModeScaleAspectFit; 
    } 
    if (count == 3) 
    { 
     [imageView setFrame:CGRectMake(10, 100, 90, 90)]; 
     imageIndex = indexPath.item; 
    } 
    if (count == 4) 
    { 
     [imageView setFrame:CGRectMake(10, 100, 130, 130)];//2lines 
     imageIndex = indexPath.item; 
    } 
    if (count == 5 || count == 6) 
    { 
     [imageView setFrame:CGRectMake(10, 100, 90, 90)];//2lines 
     imageIndex = indexPath.item; 
    } 
    if (count > 6) 
    { 
     [imageView setFrame:CGRectMake(10, 100, 100, 100)];//2lines 
     imageIndex = indexPath.item ; 
     //cell.layout.itemSize = CGSizeMake(50, 50); 
    } 


    imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];  

    [cell.contentView addSubview:imageView]; 


    return cell; 
} 

回答

0

请参阅我的answer here。你需要继承你的单元格。

发生了什么是您将现有单元出列,然后在另一个子视图中添加它。这里的大多数用户都认为,解决这个问题的唯一方法是通过主要的子类方法。它更快更“正式”,但是,您可以在工具箱中添加另一个工具:

相反,您可以测试子视图是否已经添加了WITHOUT子类,并将其粘贴到逻辑块中。以下是您可以做的事情:

BOOL doesContain = [cell.subviews containsObject:imageView]; 
if (!doesContain) 
{ 
    [cell.contentView addSubview:imageView]; 
} 
imageView.image = [DataManager imageFromPath:[self test:collectionView][imageIndex]];