UICollectionView - Where to design each cell. Frames given doesnt work properly - ios

I want to create 3 cells in each row, with width & height 104.0f each. But I am getting the output as shown in the screenshot.
The code used is given below. Since I am new to UICollectionView, I have no idea how to implement this. Where do I design the cells incase if frames are different for all?
- (void)viewDidLoad
{
....
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
dealListCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(1.0f, 1.0f, 320.0f, 525.0f) collectionViewLayout:layout];
[dealListCollectionView setDataSource:self];
[dealListCollectionView setDelegate:self];
[dealListCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[dealListCollectionView setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:dealListCollectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(104, 104);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (UIEdgeInsets)insetsForItemAtIndexPath:(NSIndexPath *)indexPath {
return UIEdgeInsetsMake(2, 2, 2, 2);
}

Click on your CollectionView, Go to the Size Inspector and change the default value of Min Spacing For Cells and For Rows. For your size 104x104 it have to be 4px.

The below code solved my problem :
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 2.0;
}

Related

The UICollectionViewCell do not flow according to common sense

I set my UICollectionView's delegate methods:
#pragma mark - collectionView delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//CGPoint point = self.collectionView.frame.origin;
//CGRect rect = CGRectMake(point.x, point.y, self.collectionView.bounds.size.width, 70*2);
// height
/*_heightOfCollectionView.constant = kHeight_Collection_Cell * 6;
_heightOfContents.constant = collectionView.frame.origin.y + 230 + _heightOfCollectionView.constant;*/
return 12;//_recommendDataArr.count;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewFlowLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
float width = (self.view.bounds.size.width / 2);
float height = 89; // 89
return CGSizeMake(width, height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HomeGMCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"HomeGMCell" forIndexPath:indexPath];
if (cell == nil) {
cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"HomeGMCell" forIndexPath:indexPath];
}
return cell;
}
The minimumLineSpacingForSectionAtIndex is 0:
And you see I return the Cell's size's width is:
(self.view.bounds.size.width / 2) // attention: no matter is `(self.view.bounds.size.width / 2) - ?(?=1,2,3,4)` it all get the below issue:
But I get the result:
It did not flow to the 2 cols. it flows to one flow.
But if I set the Cell's size's width (self.view.bounds.size.width / 2) - 5 in the sizeForItemAtIndexPath method, I will get the below effect:
It shows two cols, it my requirement, but however there comes an other issue is, the right has a 10px's space.
How to avoid this issue in UICollectionView?
Add this method for padding .
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(SCREEN_WIDTH/2 -5, 89);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 10.0f;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0f;
}

UICollectionView Cell Width Issue

I have a UICollectionViewwith custom height and width but when i launch the app, all those cells which are initially visible to screen are perfectly set with respect to my give Height and Width but when i scroll down it reduce the cell width.
Here are the methods for setting Height and Width, Spacing etc.
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 5.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 5.0f;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
[self.collectionView setNeedsLayout];
[self.collectionView layoutIfNeeded];
CGFloat width = self.collectionView.frame.size.width;
return CGSizeMake((width/2)-5, 220);
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return [listOfRetailers_ count];
}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
BDRetailerCollectionViewCell *newCell = (BDRetailerCollectionViewCell*)cell;
UIFont *font = newCell.brandName.font;
[newCell.brandName setFont:[font fontWithSize:12]];
[newCell setNeedsLayout];
[newCell layoutIfNeeded];
}
Remove this line from this method
(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
[self.collectionView setNeedsLayout];
[self.collectionView layoutIfNeeded];

UICollectionView can't create perfect 3x3 grid for iPhone 5. Is it possible?

I am having trouble creating a perfect 3x3 grid using a UICollectionView on the iPhone 5. Basically I want there to be 0 spacing between each cell. When I do it on the iPhone 6, it works because the width can be divided perfectly into 3 (375px/3 = 125px). However, with the iPhone 5, 320px does not divide into 3 perfect.
Therefore on the iPhone 5, I get little lines like this:
But as you can see, it's fine on the iPhone 6:
Is there a solution to this or is it just impossible?
Here is my setup:
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *feedLayout = [[UICollectionViewFlowLayout alloc] init];
//feedLayout.headerReferenceSize = CGSizeMake([self.view frameSizeWidth],235);
self.profileCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, navigationbarHeight + [_statsHeader frameSizeHeight], self.view.frameSizeWidth, self.view.frameSizeHeight - navigationbarHeight - [_statsHeader frameSizeHeight] - tabBarHeight) collectionViewLayout:feedLayout];
[self.profileCollectionView setDataSource:self];
[self.profileCollectionView setDelegate:self];
[self.profileCollectionView registerClass:[ANListenFeedCollectionViewCell class] forCellWithReuseIdentifier:#"feedCell"];
[self.profileCollectionView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:self.profileCollectionView];
self.automaticallyAdjustsScrollViewInsets = NO;
}
//****************************************
//****************************************
#pragma mark - UICollectionViewDelegate
//****************************************
//****************************************
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 15;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (ANListenFeedCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ANListenFeedCollectionViewCell *cell = (ANListenFeedCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"feedCell" forIndexPath:indexPath];
cell.backgroundColor=[UIColor blackColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake([self.view frameSizeWidth]/3.0f, [self.view frameSizeWidth]/3.0f);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return 0;
}
Ah, I figured out a work around. Make the middle box slightly smaller in width so that it fits. It's not perfect, but oh well.
if(indexPath.row % 3 == 0){
return CGSizeMake(107, 107);
}
if(indexPath.row % 3 == 1){
return CGSizeMake(106, 107);
}
if(indexPath.row % 3 == 2){
return CGSizeMake(107, 107);
}

Auto fit all UICollectionViewCell from data source to screen without scrolling

I've an array of UIColor's in my data source, what I'm trying to do is to fit all cells' size so they can all be seen on screen without scrolling.
I don't know how to do it, so far I've:
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
UIColor *cellColor = (UIColor *)self.dataSource[indexPath.row];
cell.backgroundColor = cellColor;
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UIImageView class]]) {
if ([[(UIImageView *)view image] isEqual:[UIImage imageNamed:#"V_BG.png"]]) {
[view removeFromSuperview];
}
}
}
if ([cellColor isEqual:self.existingColor]) {
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"V_BG.png"]];
img.frame = CGRectMake(0, 0, 30, 30);
img.center = cell.contentView.center;
img.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:img];
}
return cell;
}
#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[[NSNotificationCenter defaultCenter] postNotificationName:UIColorDidChangeNotification
object:[NSString stringWithFormat:#"%ld", (long)indexPath.row]];
[self.navigationController popViewControllerRetro];
}
#pragma mark Collection view layout things
// Layout: Set cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat fixSize = ([UIScreen mainScreen].bounds.size.width / 3) - 2;
return CGSizeMake(fixSize, fixSize);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 2;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 2;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 0, 0, 0);
}

iOS: Scroll one cell at a time

I have horizontal scrolling set up now I would like for it to sort of scroll/snap one cell at a time when scrolling side to side.
Could someone shed some light? Here's the code I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
[collectionView setPagingEnabled:YES]; might do the trick?
http://adoptioncurve.net/archives/2013/04/creating-a-paged-photo-gallery-with-a-uicollectionview/

Resources