why uicollectionview not showing anything - ios

trying to use UICollectionView as follows
the .h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>
#property(strong, nonatomic) UICollectionView *collectionView;
#end
and the .m
#import "ViewController.h"
#interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
#end
#implementation ViewController
#synthesize collectionView;
- (void)viewDidLoad {
[super viewDidLoad];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewLayout *layout = [[UICollectionViewLayout alloc] init];
collectionView=[[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[collectionView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:collectionView];
}
-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 15;
}
-(UICollectionViewCell *) collectionView:(UICollectionView *)mycollectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[mycollectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor redColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(50, 50);
}
#end
I was intended to show a list of green rect on a red background, but only the red background shows, what was I missing?

Use "UICollectionViewFlowLayout" in place of "UICollectionViewLayout" while initializing Layout.
UICollectionViewLayout *layout = [[UICollectionViewFlowLayout alloc] init];
link

Related

objective-c Embedded UICollectionViewController shows 0 items

I'm trying to have a UICollectionViewController inside my UIViewController.
Here's how I set it up
TagsCollectionViewController* tagsCollectionViewController = [[TagsCollectionViewController alloc] initWithCollectionViewLayout:[UICollectionViewLayout new]];
UIView *tagsView = tagsCollectionViewController.view;
[self.view addSubview:tagsView]; // setting up constraings too
And this is my TagsCollectionViewController
#interface TagsCollectionViewController () <UICollectionViewDelegateFlowLayout>
#end
#implementation TagsCollectionViewController
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
self.collectionView.backgroundColor = [UIColor yellowColor];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
#pragma mark <UICollectionViewDelegateFlowLayout>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(50, 50);
}
#end
What's strange is that I get yellow view so my collection seems to be working, however I don't get to see any items. But when I present whole controller everything works fine. So the problem occurs only when I'm trying to embed it. What can be the issue?
EDIT: Related: Adding UICollectionViewController to UIViewController Not Working
It looks as if it's a problem with estimating the size of items in the collection. I was able to get results by using the following code after creating a similar example and noticing that cells weren't being requested.
#interface ViewController ()
#property (nonatomic, strong) TagsCollectionViewController *tags;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
layout.itemSize = CGSizeMake(50.0, 50.0);
self.tags = [[TagsCollectionViewController alloc] initWithCollectionViewLayout:layout];
UIView *tagsView = self.tags.view;
[self.view addSubview:tagsView];
}

Imageview is not showing up in a programmatically created collectionview

I am having a hard time figuring out why cell.imageview property doesn't show me image below. UIImage is not nil, cells are created, if I assign a background to cell, it is displayed which proves I am creating collectionview and my cells correctly. I programmatically create my collectionview and don't use any IB or interface builder. I have done this lots of times before, but I am really scratching my head with this one.
Thanks in advance.
#interface MainViewController : UIViewController
#property(strong, nonatomic) UICollectionView *collectionView;
#end
#interface MainViewController ()<
UICollectionViewDataSource, UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate>
#property(nonatomic, strong) MHTransitionShowDetail *interactivePushTransition;
#property(nonatomic) CGPoint lastPoint;
#property(nonatomic) CGFloat startScale;
#property(strong, nonatomic) NSMutableArray *carImages;
#end
#implementation MainViewController
- (instancetype)init {
self = [super init];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_carImages = [#[
#"chevy_small",
#"mini_small",
#"rover_small",
#"smart_small",
#"highlander_small",
#"venza_small",
#"volvo_small",
#"vw_small",
#"ford_small",
#"nissan_small"
] mutableCopy];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewFlowLayout *layout =
[[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(106, 106);
layout.minimumLineSpacing = 1.0;
layout.minimumInteritemSpacing = 1.0;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame
collectionViewLayout:layout];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
self.collectionView.alwaysBounceVertical = YES;
[self.collectionView setContentInset:UIEdgeInsetsMake(20, 10, 10, 10)];
[self.collectionView registerClass:[IKGCollectionViewCell class]
forCellWithReuseIdentifier:#"myCell"];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma CollectionView & FlowLayout methods
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(120, 120);
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return self.carImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
IKGCollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:#"myCell"
forIndexPath:indexPath];
long row = [indexPath row];
UIImage *image = [UIImage imageNamed:self.carImages[row]];
cell.imageView.userInteractionEnabled = YES;
cell.imageView.image = image;
NSLog(#"cell.image view %#", image);
// [self makeCell:(IKGCollectionViewCell *)cell atIndexPath:indexPath];
return cell;
}
}
#end
#interface IKGCollectionViewCell : UICollectionViewCell
#property(nonatomic, strong) UIImageView *imageView;
#end
#implementation IKGCollectionViewCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
NSLog(#"self.bounds is %#", NSStringFromCGRect(self.bounds));
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = self.contentView.bounds;
}
#end
You missed to add the cell's imageView as a it's subview.
So in cell's initWithFrame
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
//Add imageView as subview
[self addSubview:self.imageView];
NSLog(#"self.bounds is %#", NSStringFromCGRect(self.bounds));
}
return self;
}

Creating a UICollectionView within a subview

I have a view controller (that sits in a tab bar controller), but need to add a collection view to that controller. I see lots of tutorials on google but they all seem to point to creating a UICollectionViewController and starting in viewDidLoad. But how do I do it in a subview?
I have my view controller.m file like so:
- (void) createView // called from viewDidLoad
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 64.0, screenWidth, screenHeight)];
[scrollView.layer addGradient];
ACollectionView *theview = [[ACollectionView alloc] init];
[self.view addSubview:theview];
}
Next I started a UICollectionView subclass called ACollectionView.h
#interface ACollectionView : UICollectionView
#end
And the .m file is this:
#import "ACollectionView.h"
#implementation BestCollectionView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
Where do I start the collection view, in the initWithFrame?
I am trying to follow this:
Creating a UICollectionView programmatically
Is my paradigm correct?
#property (nonatomic, strong) UICollectionView *collectionView;
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
self.collectionView = [[UICollectionView alloc] initWithFrame:self.yourSubview.frame collectionViewLayout:layout];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[self.yourSubview addSubview:self.collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 50;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 100);
}

UICollectionView Crashes on Scroll (lldb)

I'm trying to add a uicollectionview as a subview into a View, called CollectionSpace, by calling it from Storyboard.
UIViewController *CollectionVC =[self.storyboard instantiateViewControllerWithIdentifier:#"CollectionVC"];
CollectionVC.view.frame = CGRectMake(CollectionSpace.frame.origin.x-276, CollectionSpace.frame.origin.y-87, CollectionSpace.frame.size.width+2, CollectionSpace.frame.size.height-2);
[CollectionSpace addSubview:CollectionVC.view];
The storyboard has a Collection View where the delegate and Source, as the uicollectionview have been linked to the owner view controller. Here is the code:
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) IBOutlet UICollectionView *collectionView; //linked
#end
#implementation ViewController
- (void)viewDidLoad
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 100;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (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(150, 150);
}
So this works fine, it loads the uicollectionview and I can see the cells. The problem comes when I scroll. Once I scroll, the app crashes. (lldb)
I tried to call differently by importing the view controller and initiate it like this:
UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setItemSize:CGSizeMake(200, 140)];
[aFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
ViewController *CollectionVC = [[ViewController alloc]initWithCollectionViewLayout:aFlowLayout];
But the uicollectionview doesn't show up.
Any idea what could be causing the crash?

how to scroll collectionview cell horizontally and vertically..i am using 9 cell collectionview

I am totally new for collection view, please help me with scrolling cell vertically and horizontally in collection view :
viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *collectionView;
}
#end
viewcontroller.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame
collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
[collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:#"cellIdentifier"];
[collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:collectionView];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
- (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(100, 100);
}
#end
I'm a little late here, but just in case anybody else looks this up, you can easily set your collection view scroll direction by using UICollectionViewFlowLayout.
Create an instance of UICollectionViewFlowLayout
Set the scrollDirection to UICollectionViewScrollDirectionHorizontal
Rejoice.
-(instancetype) init
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.itemSize = CGSizeMake(106, 106);
layout.minimumInteritemSpacing = 1;
layout.minimumLineSpacing = 1;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self= [super initWithCollectionViewLayout:layout];
return self;
}
you can scroll the collectionview programmaticaly with the follwing methods of UICollectionView class:
[collectionView scrollsToTop];
[collectionView scrollToItemAtIndexPath:<#(NSIndexPath *)#> atScrollPosition:<#(UICollectionViewScrollPosition)#> animated:<#(BOOL)#>];
[collectionView scrollRectToVisible:<#(CGRect)#> animated:<#(BOOL)#>];

Resources