I'm trying to create my own custom UICollectionView
For starters, I added a CollectionView to my Storyboard.
The reason for the PURPLE is because at first I expereinced my contorller not showing at all. So I turned the CELL grey and noticed it was the item not visible after also turning the Collection View purple.
I'm trying to populate my collection view with a unique image and label. This is my beginning of the journey.
Here is my CollectionViewController
#import "LoadZoneViewController.h"
#import "ZoneCollectionViewCell.h"
#interface LoadZoneViewController ()
#end
#implementation LoadZoneViewController
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
// Register cell classes
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
return 12;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
return 12;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
return cell;
}
#pragma mark <UICollectionViewDelegate>
#end
I defined a custom UICollectionViewCell as well, but I thought I should worry about that after I fix this issue. The current issue is simply that the label doesn't appear, the View has a scrollbar to the bottom, but I'm not seeing 12 labels.
Try adding delegates to your class interface
#interface LoadZoneViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
Add this to your viewDidLoad
[CollView registerNib:[UINib nibWithNibName:#"ZoneCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"YourCellIdentifier"];
And do this is in your cellForItemAtIndexPath
NSString *identifier = #"YourCellIdentifier";
ZoneCollectionViewCell *cell = (ZoneCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
Related
i have collectionView. it shows cell in vertically.
in the start the collectionView looks like this
start of the collectionview
at the end it looks like this
end of collectionView
so it basically doesnt show up the whole cell in the end
the code for collection class is
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - CollectionView Datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ViewControllerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
return cell;
}
#end
You have to specify the cell size in your storyboard.
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
float width = 100; //change to your needed width
float height = 100; //change to your needed height
return CGSizeMake(width, height);
}
try this code it will help you
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"cellIdentifier";
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
return cell;
}
and change identifier name in attribute inspector as shown in image:
I have a table view controller in which the cells don't show up:
I have added UiViewDataSource and UITableViewDelegate as shown below:
myViewController:UITableViewController<UITableViewDataSource,UITableViewDelegate>
In the storyboard as well as in the code I have set
self.tableView.delegate =self;
self.tableview.datasource = self;
My numberOfRowsInSection returns the value 30 (verified with logs) and numberOfSectionsInTableView returns 1 (verified with logs)
I have also printed the tableView header and footer view frame,height and width and they all have the value 0.0000
NSLog(#"The header frame size is%f" ,self.tableView.tableHeaderView.frame.size.height);
NSLog(#"The header frame size is%f" ,self.tableView.tableHeaderView.frame.size.width);
NSLog(#"The header frame size is%f" ,self.tableView.tableFooterView.frame.size.height);
NSLog(#"The header frame size is%f" ,self.tableView.tableFooterView.frame.size.width);
This is the firstController of a navigation controller.
The numberOfSectionsInTableView method is called first, then the numberOfRowsInSection and then the heightForRowAtIndexPath is called.
The cellForRowAtIndexPath method contains:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *) [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureCustomCell:[self.dataSource objectAtIndex:indexPath.row]];
return cell;
}
configureCustomCell configures the different properties in the cell.
I tried all the solutions given in similar questions but none of them seem to be working. What am I missing?
if you have configured UITableViewDataSource and UITableViewDelegate protocols inside your ViewController.h file, then there is no need to use self.tableView.delegate =self;
self.tableview.datasource = self;
if you have configured UITableView inside your storyboard.
you need to configure your CustomTableViewCell with your tableView's cell property.
step - 1
select your tableView Cell and inside identity inspector define your CustomTableViewCell class.
step - 2
now provide cell identifier. (i have used "customCell")
import your CustomCell class inside your ViewController.m file. Just created demo app as per your reference. Hope it solves your query.
#import "ViewController.h"
#import "CustomTableViewCell.h" //CustomTableViewCell class
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 30; //used staticCells 30
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"customCell"; //as defined in cell identifier (as per step -2)
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
// configure your custom cell
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Is anyone having a problem with Xcode 6 when adding a Collection View Controller to storyboards and naming the custom class?
-> I drag a Collection View Controller to my scene -> add a new file -> name it "LCCollectionViewController" with a subclass of UICollectionViewController.
Next, I select the Collection View Controller in storyboard, and name the custom class LCCollectionViewController in the identity inspector.
Problem: The document outline still shows the name as Collection View Controller.
Code just in case:
LCCollectionViewController.m
#interface LCCollectionViewController ()
{
NSArray *theImages;
}
#end
#implementation LCCollectionViewController
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
moonImages = [NSArray arrayWithObjects:#"image1.gif", #"image2.gif", #"image3.gif", #"image4.gif", nil];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [moonImages count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
UIImageView *theImageView = (UIImageView *)[cell viewWithTag:100];
theImageView.image = [theImages objectAtIndex:indexPath.row];
return cell;
}
#end
I encountered same bug in xcode6. What helped me was to change subclass to any other viewcontroller subclass you have set up already (perhaps create temporary one if none is available) and then change it back to LLCollectionViewController.
I'm trying to create a simple View : a tableView which takes like 80% of the screen, and a collectionView with 4 rows at the bottom. Everything works fine with my tableView but I can't get things right with my collectionView. I always get the following exception :
Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'could not dequeue a view of kind: UICollectionElementKindCell with
identifier simpleCollectionCell - must register a nib or a class for the
identifier or connect a prototype cell in a storyboard'.
I've been looking in my View Controller : there are 2 refering outlets for my collectionVIew : datasource and delegate. So it's fine. And I'm using the method "withReuseIdentifier" so I don't have to declare an identifier in the nib. I've been trying to create a property for collectionView, but it changes nothing.
So I really don't see what I am missing ...
.h :
#import <UIKit/UIKit.h>
#interface MainViewController : UIViewController <UITableViewDelegate,
UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
#end
.m :
#import "MainViewController.h"
#interface MainViewController ()
#end
#implementation MainViewController {
NSArray *recipes;
NSArray *images; }
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self; }
- (void)viewDidLoad {
[super viewDidLoad];
recipes = [NSArray arrayWithObjects:#"Label1", #"Label2", #"Label3", #"Label4", #"Label5", #"Label6", #"Label7", #"Label8", #"Label9", #"Label10", #"Label11", #"Label12", #"Label13", #"Label14", #"Label15", nil];
images = [NSArray arrayWithObjects:#"img1", #"img2", #"img3", #"img4", nil];
// Do any additional setup after loading the view. }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
#pragma mark TableView methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [recipes count]; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
#pragma mark CollectionView methods
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection: (NSInteger)section {
return [images count];
}
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView
dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
return cell;
}
#end
Thanks in advance !
Make sure you have set the proper Custom Class(if any) as well as the Cell Identifier for the Collection View Cells in the Interface builder. Check this Screenshot.
This is for the Custom Class and the one below is for Cell Identifier.
The Identifier in your case is "cell". Make sure you have these set. They might be the problem here. Also the Class name of your CollectionViewCell and the one in your cellForItemAtIndexPath should be same.
Hope this helps.
You have To register your collection view cell
[self.dashBoardCollection registerNib:[UINib nibWithNibName:#"DashBoardCell" bundle:nil] forCellWithReuseIdentifier:#"DashBoardCell"];
may be in identity Inspector your are missing to name the Class
Oky u can try with this, just a simple way,
First create a new file in user interface select view and name as NibCell.
Then select the device (choose any one of them).
Delete what ever the content in the view.
Just drag and drop a new CollectionViewCell that's it.. :)
in the collectinview cell u can add image view (or any other objects u want) and set its tag to access.
for example
in viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
//....other code
//nib register
UINib *cellNib = [UINib nibWithNibName:#"NibCell" bundle:nil];
[_collectionView registerNib:cellNib forCellWithReuseIdentifier:#"Cell"];
}
and in this method
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell"; //u know the identifier
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; //get the cell
UILabel *titleLabel = (UILabel *)[cell viewWithTag:100]; //for example i put a label and set it's tag to 100 and i an accessing it
[titleLabel setText:cellData];
return cell;
}
And I have to ask this as well. Been looking through the code for hours now and tried everything, but why does my old picture not get deselected? The user should be able to select one icon only. The selected icon is saved in Core Database. So this icon is also preselected when opening this view. However this item doesn't get deselected when he selects a new icon..why?
#import "IconSelectionCollectionViewController.h"
#import "IconSelectionCell.h"
#interface IconSelectionCollectionViewController ()
#property (nonatomic, strong) NSArray *icons;
#end
#implementation IconSelectionCollectionViewController
#synthesize mainCategory = _mainCategory;
#pragma mark Initialize model
- (void)setMainCategory:(MainCategory *)mainCategory
{
//TODO
_mainCategory = mainCategory;
self.title = mainCategory.name;
}
#pragma mark View setup
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:IconSelectionCell.class forCellWithReuseIdentifier:#"IconCell"];
// Do any additional setup after loading the view.
self.icons = [NSArray arrayWithObjects:#"DefaultIcon", #"Car", #"Diploma", #"Earth", #"Flight", #"Home", #"Pen", #"Scooter", #"Ship", #"Train", nil];
self.collectionView.allowsSelection = YES;
self.collectionView.allowsMultipleSelection = NO;
}
#pragma mark Data source delegate methods
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.icons.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"IconCell";
IconSelectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.iconImageView.image = [UIImage imageNamed:[self.icons objectAtIndex:indexPath.row]];
if([self.mainCategory.icon isEqualToString:[self.icons objectAtIndex:indexPath.row]]){
cell.selected = YES;
}
return cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
#pragma mark Collection View Delegate methods
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.mainCategory.icon = [self.icons objectAtIndex:indexPath.row];
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize itemSize;
itemSize.height = 62;
itemSize.width = 62;
return itemSize;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 10, 10, 10);
}
#end
I am not incredibly familiar with collection views, but they are very similar to table views and this is a problem that sometimes occurs with UITableviews, so I will work through a couple possible solutions.
One way to do this would be to call [self.collectionView reloadData] after the selection code. This should cause the collectionView to redraw the images, setting only one of them to be the selected image.
However, I'm not confident this would solve the problem, since dequeue reusable cells could cause the "selection" value to be set for the reused cell. So alternatively, I'd imagine you could grab both cells using UICollectionView's method cellForItemAtIndexPath: and then set their selected value in the way you set them in the main method. That may just work, but if it does not, try doing that and calling reload data again.