UICollectionView Crashes on Scroll (lldb) - ios

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?

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];
}

why uicollectionview not showing anything

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

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);
}

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)#>];

IOS, UICollectionview

New to IOS and objective C and been doing several tutorials, with adding buttons, textfields etc. Been looking alot on stackoverflow, which has been very helpful :) Now I am trying to create a collectionview within a frame and then put a button below it. I can get a button to show up, and the top half of the screen where I have defined the collection to appear turns black, while the rest goes white, as intended. Currently the collection should come up with a label telling me the cell number, so very basic. Not sure if I'm setting up my views wrong, or if there is something wrong with my CellViews.
I have created a mainView controller, where I have defined my button and collection view, like this:
MainViewController.h:
#interface MainViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
#property(nonatomic, strong) IBOutlet UICollectionView *collectionView;
#property(nonatomic, strong) IBOutlet UIButton *diaryButton;
- (id)init;
My .m looks like this:
#implementation MainViewController
#synthesize collectionView;
#synthesize diaryButton;
- (id) init {
self = [super init];
if (self) {
CGRect buttonFrame = CGRectMake(10, 250, 70, 30);
diaryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[diaryButton setFrame:buttonFrame];
[diaryButton setTitle:#"Diary" forState:UIControlStateNormal];
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(64.0, 64.0);
layout.minimumInteritemSpacing = 4;
layout.minimumLineSpacing = 4;
layout.scrollDirection = UICollectionViewScrollPositionCenteredVertically;
layout.sectionInset = UIEdgeInsetsMake(2.0, 2.0, 2.0, 2.0);
CGRect collectionFrame = CGRectMake(0, 0, 320, 200);
collectionView = [[UICollectionView alloc] initWithFrame:collectionFrame collectionViewLayout:layout];
[self.view addSubview:collectionView];
[self.view addSubview:diaryButton];
//[self.view setBackgroundColor:[UIColor blueColor]];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"ID"];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"ID" forIndexPath:indexPath];
UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
label.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:#"%d", indexPath.row];
[cell.contentView addSubview:label];
return cell;
}
/////////////////////////////////////////////////////////////////////////////////
// collection view delegate methods ////////////////////////////////////////
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"cell #%d was selected", indexPath.row);
}
My appdelegate looks like this:
#synthesize mainVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.mainVC = [[MainViewController alloc] init];
[self.window addSubview:mainVC.view];
//self.window.rootViewController = mainVC;
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Any help will be greatly appreciated.
I don't know exactly why your collection isn't showing anything, but you're missing the numberOfSectionsInCollectionView: method from the UICollectionViewDataSource. The collection view might be assuming that the number of sections is 0.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UICollectionViewDataSource_protocol/Reference/Reference.html
Also, did you set the datasource and delegate to self for your UICollectionView?

Resources