Reputation: 1699
I have tried with story board to create a collection view with image. Totally i have 6 images. So per row i need to show 2 images. And no gap should be in between two images in one row. when i open in all screen, it should be two image for single row. how to add this in my code:
ViewController.m :
#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()
@end
@implementation ViewController {
NSArray *arrayImages;
NSArray *arrayLabel;
}
@synthesize MycollectionView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[self MycollectionView]setDataSource:self];
[[self MycollectionView]setDelegate:self];
arrayImages =[[NSArray alloc] initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png",@"image5.png",@"image6.png", nil];
arrayLabel = [[NSArray alloc] initWithObjects:@"image1",@"image2",@"image3",@"image4",@"image5",@"image6", nil];
}
// datasource and delegate method
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [arrayImages count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
CustomCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[[Cell myimage] setImage:[UIImage imageNamed:[arrayImages objectAtIndex:indexPath.item]]];
[[Cell myLabel]setText:[arrayLabel objectAtIndex:indexPath.item]];
return Cell;
}
CustomCell.h
@interface CustomCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *myimage;
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@end
Upvotes: 0
Views: 1851
Reputation: 83
For Swift 5:
First set the collectionview's cell size from the sizeInspector,
then add the following code. Check image for clarification
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
// do some codess..
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Selected Cell: \(indexPath.row)")
navigationController?.popViewController(animated: true)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/2, height: collectionView.frame.size.width/2)
}
Upvotes: 0
Reputation: 501
To set dynamically use collection view delegate methods
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width/2, height: collectionView.frame.size.width/2)
}
It creates collection view cell of equal width and height i.e. Square
Upvotes: 1