I am trying to create a collectionview with 3 items on every row in objective c. Here is my code:
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return items.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
collectionView.backgroundColor = [UIColor clearColor];
NSString* buf = items[indexPath.row];
CollectionCell *cell = (CollectionCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
[cell loadCell:buf];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
UIColor *leftColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.5];
cell.layer.cornerRadius = ((screenBounds.size.width)*0.25)/2;
cell.layer.borderColor = leftColor.CGColor;
cell.layer.borderWidth = 2;
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat boxWidth;
boxWidth = ((screenBounds.size.width)*0.25);
bottomSpacing = 17;
return CGSizeMake(boxWidth, boxWidth);
}
It look like the left but I want it to look like the right image.
What am I doing wrong and is it possible to fix it?
2
Answers
To represent 3 columns with collectionview, each cell has to be width of 1/3 of the display. (Since 1/3 + 1/3 + 1/3 = 1)
So try the code below, I have changed the width of the cell:
You can try this –