我在我的collectionView中有雙重項目。我該如何解決它?UICollectionView雙重項目
我有UITableViewCell
其中UICollectionViewCell
裏面。
我根據它的數量設置了元素的大小和方向。
我的手機
- (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;
}