我已经在这方面工作了一段时间,不知道如何让我的单元格正确显示。在这个问题上有很多Stack Overflow问题,但我似乎不能根据我的情况来解决这个问题。我在UITableViewController的顶部有一个CollectionView (而不是在表视图单元格中)。我试图让我的集合视图单元格显示3个水平行,我需要它来调整以适应不同的iPhone和iPad屏幕大小。我附上了两张照片。一个显示我的故事板视图控制器,另一个显示其外观的模拟器。单元格行之间总是有很大的间隙。我想它显示像一个网格(像iPhone照片应用程序)。下面是我的UICollectionView的代码:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _foursquarePhotoArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"photoCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *wineryImageView = (UIImageView *)[cell viewWithTag:100];
Photo *photo = [_foursquarePhotoArray objectAtIndex:indexPath.row];
dispatch_async(dispatch_get_main_queue(), ^{
[wineryImageView setImageWithURL:[NSURL URLWithString:photo.photoURLString] placeholderImage:[UIImage imageNamed:@"Grapes"]];
});
return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
int numberOfCellInRow = 5;
CGFloat cellWidth = collectionView.bounds.size.width/numberOfCellInRow;
return CGSizeMake(cellWidth, cellWidth);
}


发布于 2016-03-10 23:03:42
试试这个:
- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0; // This is the minimum inter line spacing, can be more
}发布于 2016-03-10 23:11:06
如果您使用的是UICollectionViewFlowLayout (不是自定义布局),那么在视图控制器中,您可以通过collectionView (类似于self.collectionView.collectionViewLayout)来调整布局的属性,特别是看看minimumInteritemSpacing和minimumLineSpacing属性。
关于布局的scrollDirection,minimumInteritemSpacing和minimumLineSpacing控制不同的空格,所以您可以尝试将它们都设置为0,或者只对它们进行调整。
如果您的集合视图有一个笔尖/故事板,并且不使用自定义布局,那么您可以在大小检查器中调整这些值:
就在这里,->

我不确定我是否正确理解了这一点,但您是否也有关于单元大小的问题,或者这只是次要信息?
发布于 2016-08-06 20:11:17
你需要做的就是根据需要设置你的最小间距(检查先把它设置为0 0),同时添加单元格的高度和宽度如下:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
return CGSizeMake(collectionView.frame.size.width/5, collectionView.frame.size.height/3)
}还要检查imageView的约束是否为0,0,0,0 (顶部,底部,前导,尾随)。
备注:-如果你设置了一些最小间距的值,那么你必须从单元格的大小中减少它。例如,如果你把2作为单元格的最小间距,那么就从宽度(集合View.frame.size.width/5- 2)中减少它,假设你取3作为行的最小间距,然后从高度(集合View.frame.size.height/5- 3)中减少它。基本上,"for cells“是一行中单元格之间的水平间距,而"for line”是一列中单元格之间的垂直间距。
https://stackoverflow.com/questions/35919926
复制相似问题