话不多说,直接上干货
#pragma mark - private method
- (CGFloat)collectionViewCellWidth:(UICollectionView *)collectionView itemCountPerRow:(NSInteger)count indexPath:(NSIndexPath *)indexPath {
// 处理collectionView小数点bug,因为collectView是智能布局,当出现小数点时会随机分配宽度
NSInteger columnCount = count;
NSInteger cellWidth = round(collectionView.width / columnCount); // 四舍五入
if (indexPath.row % columnCount == 0 ) {
if (cellWidth * columnCount > collectionView.width) {
return cellWidth - (cellWidth * columnCount - collectionView.width);
} else {
return collectionView.width - cellWidth * (columnCount - 1);
}
} else {
return cellWidth;
}
}```
###这样计算cell的宽度,就不会出现因为宽度小数取舍而出现一点点边距的问题了。。。记录下。