I'm very new to iOS development, sorry for posting such a basic mistake, I just can't get my head around this.
I will tell you what I have done so far, hopefully someone can help me out here. I have not edited any code so far.
Add collection view controller (click and drag into main.storyboard)
Configure cell sizes in the attributes editor of the CollectionView
Drag UIImageView object onto each cell
Import my images into the project (drag images into sidebar and import)
Select each UIImageView and in attributes select the correct image
Having done this, my images display perfectly in the storyboard editor but when I run the app in the simulator it is just a blank screen with my specified background colour.
Screenshot of editor, Screenshot of simulator
You should add the data source method after creating a collection view controller class,
// Defines the number of cells in a section of collection view
- (NSInteger)collectionView:(UICollectionView *)cv numberOfItemsInSection:(NSInteger)section;
{
return numberOfItems;
}
// Defines the cells in the collection view
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
// Gallerycell is the custom collection view cell class holds the UIImage View
GalleryCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"GalleryCell" forIndexPath:indexPath];
// getting the image
cell.cellImage.image = [UIImage imageNamed:[dataSourceArray objectAtIndex:indexPath.row]];
return cell;
}
You should implement datasource methods for displaying a cell. Doesn't meter if it's static or dynamic cells.
Related
I faced an issue an take lot of time but cannot figure out the issue:
I created a UICollectionView with very simple content, one lable to indicate cell_number, and when a cell is selected, the background of cell is changed to orange.
I create a custom cell with xib file for design (very simple one). And I load this cell from my UICollectionView in my DemoUICVViewController.m file normally as many people did with some steps like:
- declare the custom cell class in ViewDidload:
[self.collectionView registerClass:[MyCustomCell class] forCellWithReuseIdentifier:#"MyCell"];`
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell * myCell = (MyCustomCell*)[cv dequeueReusableCellWithReuseIdentifier:#"myCell" forIndexPath:indexPath];
myCell.lable.text = [arrayNumber objectAtIndex:indexPath.item];
return myCell;
}
and when selected:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// Get the cellForItemAtIndexPath and set the background coler of cell to orange
}`
Number of section I return 1, and number of item in section I return 10
All the cells in UICollectionView are display correctly. But when I tap to select a cell I see not only that cell is orange, but also some other cells also change to orange for e.g cell 5 and cell 9.
I debug but the didSelectItemAtIndexPath only get called once and at the right index, very strange when some other cells also be orange.
I am not sure if the problem is of Reusability of cell identifier, could someone give me some advise.
I attached photo for e.g here:
Image describes
Finally I found the root cause to fix: add this line to check to
collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
...
if(myCell.selected)
// set background of cell to orange;
else
// set white background;
...
}
Deployment target 7.0,
Running on iOS7.1,
Xcode 5
I have a UIImageView "comicImage" and UILabel "comicTitle" set up using IB on the UICollectionViewCell.
comicTitle uses these settings:
The IBOutlet is properly hooked up. The view cell is properly registered to the collectionView. I have output the title in
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
on the console and the text of the title did get assigned to comicTitle.
When I run the app, comicImage shows up, but comicTitle does not show up. I have ensured that the alpha of comicTitle is set to 1.0.
This same piece of code worked in a previous app with deployment target set to iOS6.
I have no idea what went wrong. Can anyone shed light on this?
Thanks in advance!
Edit:
-(void)viewDidLoad {
UINib *comicStripCellNib = [UINib nibWithNibName:#"ComicStripViewCell" bundle:ni];
[_comicStripCollectionView registerNib:comicStripCellNib forCellWithReuseIdentifier:#"comicStripCell"];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ComicStripViewCell *comicStripViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"comicStripCell" forIndexPath:indexPath];
comicStripViewCell.comicTitle.text = #"This is a title";
comicStripViewCell.comicImage.image = [UIImage imageNamed:#"thisImage.png"];
NSLog(#"comicStripViewCell.comicTitle.text = %#", comicStripViewCell.comicTitle.text);
return (UICollectionViewCell *)comicStripViewCell;
}
I do not think it is a problem with the code.
Some suggestions.
Apply a background color to check where your label lies
Mess with the label frame values. Give a trial and error and see whether your label has been misplaced somewhere.
If you have used constraints make sure you have given the specific constraints correctly.
Check whether your comicImage is not overlapping your label
You can actually see a preview of your xib in xcode(i.e, After you designed your cell you can confirm how your cell will actually display in the screen). Refer the below screenshot
you can do it by given the tag value below code is the sample code for UILabel tag approach
In storyBoard Drag the UILabel from the Object library in to the collection view cell
Give the Tag value 30 to UILabel from the Attributes Inspector
Get the UILabel object from the below code
write the below code in the Collection view DataSource method
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ComicStripViewCell *comicStripViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"comicStripCell" forIndexPath:indexPath];
UILabel *comicTitle = (UILabel *)[comicStripViewCell viewWithTag:30];// get the UILabel object by its tag value
comicTitle.text = #"testing"; // assign the value that you want for comicTitle
return (UICollectionViewCell *)comicStripViewCell;
}
otherwise share the piece of code that you have.
may be your label frame has been misplaced somewhere.
Please check UILabel Frame also
I am new to IOS , and I use the Xcode5.
I want to create a view like the following picture.
Can I add a View , and add the button and the collectionView cell in to the View , and add the imageView and two label which below the imageView in to collectionView cell ?
or does there has other method to implement the view like the picture ?
---------------------------------------EDIT------------------------------------------------
I have modify the xib file like the following picture.
I add the collectionView below two button , but how to add the label below image like the following first picture ?
you can add the top two buttons to the collectionView headerView using following method
// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:#"headerView" forIndexPath:indexPath];
if (kind == UICollectionElementKindSectionHeader) {
[headerView addSubview:YOURBUTTON1];
[headerView addSubview:YOURBUTTON2];
}
return headerView;
}
Yes you can implement it like you want fairly easily.
You have a couple of options, you can either use a UICollectionView and just pop it where you want it to go on top of your UIViewController along with the buttons.
The other option is to use a UIContainerView to contain your UICollectionViewController and manage it through the corresponding view controller to manage the data/delegate.
luckily I have 2 such examples handy from something I was testing recently.
Above: UICollectionView in a UIViewController with the buttons
Below: A UICollectionViewController embedded in a UIContainerView
I have a UICollectionView inside of a normal UIViewController.
Inside the collectionview I have designed the reusable UI for the collectionviewcells in storyboard.
Inside of the collectionviewcell there is a label that displays the cells indexpath.row and 5 UIButtons which if selected, change color and stay selected.
I have set up the collectionview so that if more that 30 cells are requested the collectionview will page horizontally, the collectionview layout is also horizontal.
The application runs nicely, scrolls properly and lays out cells correctly.
The problem I am having is when you select for example button A in cell 1 in the collectionviewcell (which is suppose to layout 100 cells) and page over two pages (60+ Cells) to page 3, button A in cell number 75 is selected. And further more if you scroll to the end (100 cells) and scroll back to page 3, button A in cell number 75 is on longer selected, but button A in cell number 64 is selected.
Here is some snippets of code:
cell.m - controls the action from the user.
- (IBAction)bubbleButtons:(id)sender {
for(UIButton *bubbleCell in self.bubbleButtons) {
if (bubbleCell.touchInside && !bubbleCell.selected) {
bubbleCell.selected = YES;
} else if (bubbleCell.touchInside && bubbleCell.selected) {
bubbleCell.selected = NO;
}
}
}
MainViewContoller.m - sets up cell from UICollectionViewCell made in storyboard
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Cell *cell1;
cell1 =[collectionView dequeueReusableCellWithReuseIdentifier:zCellID
forIndexPath:indexPath];
cell1.numMainLabel.text = [NSString stringWithFormat:#"%d |",indexPath.row+1];
return cell1;
I do not really understand what is wrong or what is causing this bug, I am assuming it has todo with the view being reloaded when a new part of the view becomes visible but that is just a guess. Help would be greatly appreciated.
Zach
It's probably because the reusable view is reused.
The proper way to do this is to create custom reusable view subclass.
And save the selection of those 5 button.
cell1 =[collectionView dequeueReusableCellWithReuseIdentifier:zCellID
This line here might or might not give you a new cell, it might give you a cell that is used before. So, you need to update the selection in there. Or it stay the same as the cell it's reusing.
The Uicollection only generate the cells that are been displayed at that moment, so when a cell disapier from the visible view, its been replace with the new one.
So when you seleted the cell 75 and you scroll down, until the cell 75 is no visible, and then you scroll back to cell 75, you are generating a new cell, with a new button that is not seleted because is new.
So what yo could do, is save which buttons had been selet, and in
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
ask if the button thats is been displayed at that moment need to be selected..
Hope its helps
I am just starting to implement a multiselect UICollectionView. Would the below be considered "safe" code (since i assume theres some reason it is called BackgroundView instead of AccessoryView or such) ? I had the idea to save some effort, i intend to keep track of the selected items at the indexpath for further use via an array.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//....
cell.selectedBackgroundView = someView_With_A_Checkmark_Image;
[cell bringSubviewToFront:cell.selectedBackgroundView];
//...
return cell;
}
Is it safe?? Ya of course it wont cause any error. If your backgroundView is above the contentView of the cell, then what is the significance of contentView??.
Collection view cell structure
If you select an item in the collection view, collectionView will switch the BackgroundView and Selected background view. So what you can do is give valid views as background view and selected background view upon configuring your custom cell or change any properties of the cell in didSelectItem to differentiate selection. That is better.
Then one more no need to keep track of selection using a separate array. [self.collectionView indexPathsForSelectedItems] will give you selected items path at any point of time