collectionView layout sizeForItemAtIndexPath
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger kItemNumOfRow = 3;
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)collectionViewLayout;
NSInteger row = indexPath.row;
CGFloat totalWidth = collectionView.frame.size.width - layout.sectionInset.left - layout.sectionInset.right - layout.minimumInteritemSpacing * (kItemNumOfRow - 1);
CGFloat width;
if (row % kItemNumOfRow) {
width = roundf(totalWidth / kItemNumOfRow);
} else {
width = totalWidth - roundf(totalWidth / kItemNumOfRow) * (kItemNumOfRow - 1);
}
return CGSizeMake(width, width);
}