- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ConvePayCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ConvePayCollectionCellID forIndexPath:indexPath];
if (self.dataSource.count > 0) {
NSDictionary *dict = self.dataSource[indexPath.item];
cell.infoDict = dict;
}
return cell;
}
//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(((SCREEN_WIDTH-30 * 3-40)/4), 90);
}
//调节item边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dict = self.dataSource[indexPath.item];
KKLog(@"选择了%@",dict);
}
- (void)setDataSource:(NSArray *)dataSource
{
_dataSource = dataSource;
[self.collectionView reloadData];
NSInteger count = self.dataSource.count;
// 设置collectionView 计算collectionView高度 = rows * itemWH
//万能公式: 行数= (个数-1)除以列数 + 1;
// Rows = (count - 1) / cols + 1 3 cols4
NSInteger rows = (count - 1) / 3 + 1;
if (count > 4) {
self.collectionHeight.constant = rows * 90 + rows * 10;
}
else{
self.collectionHeight.constant = 90;
}
}
网友评论