UICollectionViewCell reflection - appropriate time for taking the cells' snapshot image - uicollectionview

I'd like to add reflection to my collection view's cells. I have the method that creates a reflection, but I don't know the correct time to create the snapshot.
I have tried doing it in the collection view's -viewWillAppear:, but the cell's contentView has zero-sized frames for its subviews. I have also tried taking the snapshot during the cell's -layoutSubviews, but the subview frames are still 0.
At what point is the cell's layout set so I can take the snapshot?

The snapshot should be taken after the cell has been added to the collection view, and after the cell has been dequeued by the collection view data source. This ensures the snapshot will be up to date, containing the cell's content view at the correct moment (when it's about to be added to the collection view, and after it has just been dequeued):
CollectionViewCell.m
- (void)didMoveToSuperview {
[self updateSnapshotImage];
}
CollectionViewController.m
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellID" forIndexPath:indexPath];
[cell updateSnapshotImage];
return cell;
}

Related

custom cell for UICollectionView

I need to create an app which has a list of items, each item has an image a title, a subtitle a price and a description and I need 2 items per line (so I can't use a UITableView)
Is it possible to use a UICollectionView with custom cell in order to have the result I want or should I search another solution?
The custom cell layout should be like this:
You totally can !
Just create your custom UICollectionViewCell, then go in the "cellForItemAtIndexPath" method in order to use it.
Hope this help :)
1, create a xib file in the subclass of UIcollectionViewCell
2, register your xib in controller's viewdidload fun
[mCollectionView registerNib:[UINib nibWithNibName:#"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"Cell"];
3,
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
return cell;
}
You can definitely customise your UICollectionView's Cell. But you will have to do following steps as well:
Register your Custom Cell to your collection view :
[self.collectionView registerClass:[YourCustomClass class]
forCellWithReuseIdentifier:#"CustomCell"];
Secondly, inside your method : cellForItemAtIndexPath, you will have to call custom cell like this (as you do in tableView's custom cell too):
YourCustomClass *cell = (YourCustomClass *)[collectionView
dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
Here is a tutorial to make custom UICollectionView Cell

Collectionview cell to open new view

I have a collection view with 6 cells in it how do I have each cell open a new viewcontroller when clicked?
In order to achieve your goal you need to edit 2 files:
In the Storyboard you need to add a cell view for each static cell in your Collection View. Just drag as many UICollectionViewCells onto the Collection View. For each cell you need to define a unique reusable identifier (meaning a name like CellType1) in the Attributes Inspector when a cell is selected in the Storyboard's Document Outline. And then control-drag from each cell to the desired target view controller to create a Push segue (provided that your collection view is associated with a UINavigationController).
Create a subclass of UICollectionViewController and assign it in the Storyboard's as the Collection View Controller's class (see the Identity Inspector).
In your UICollectionViewController subclass implement the following methods:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
// Enter the number of static cells that are present in the Storyboard's collection view:
return 3;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// Enter the reusable identifiers that are defined for each cell in the Storyboard's collection view:
NSArray *cellIdentifiers = #[#"CellType1", #"CellType2", #"CellType3"];
NSInteger cellIdentifierIndex = indexPath.item;
// Make one identifier the default cell for edge cases (we use CellType1 here):
if (cellIdentifierIndex >= cellIdentifiers.count) cellIdentifierIndex = 0;
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifiers[cellIdentifierIndex] forIndexPath:indexPath];
// Configure the cell …
return cell;
}
I've created a demo app to show a full implementation: https://github.com/widescape/StaticCollectionViewCells

UICollectionView, Images inside a cell does not load until the cell is scrolled off screen

Using Xcode 6 beta 6, I have used a standard View Controller and placed a Collection view into it. Following this, I have added a prototype cell from which an array on the CollectionViewController.m file provides the images.
While the images do appear, they do not load until after that cell has been scrolled off the screen and then back on. This is stopping any images from loading when the app opens until the user scrolls down.
The images are stored locally in the file, not a Database.
Despite the lack of images, I have linked the cells to another view controller, and even though no image is displayed, the segue still operates.
Thanks for any help in advance.
I guess you are setting the UIImage on the cell image view out of the main thread. Wrap the setter code into a following snippet:
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
cell.imageView.image = {UIImage object you've got};
}];
Optionally you could also try different, single-line replacement approach, if possible:
[cell.imageView performSelectorOnMainThread:#selector(setImage:) withObject:{UIImage object} waitUntilDone:NO];
I can't find what kind of cell you mean in fact; in every case, apply the same on a particular target image view, no matter if it's a part of your cell as a property or not.
you should use willDisplayCell method to assign image.
first initial image then create cell.
here is the code :
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
return cell;}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
imageView.image = images[indexPath.row];}
And don't forget to reload collection view after your data is ready to be loaded:
[_myCollection reloadData];

Showing a specific cell each scroll back on UICollectionView

I'm trying to load a custom cell each time someone scrolls up/back on my UICollectionView then if a condition is met, allow that user to see the previous items in the UICollectionView / resume normal UICollectionView behavior.
I tried subtracting the current indexpath.row, but that isn't working and I can't wrap my head around what exactly the case needs to be to make it work.
I was able to successfully get the custom cell to render as the third item (2) in the UICollectionView (shown below), but can't figure out how to make it's index path 0 and start the collection view on index path 1.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 2) {
MyCustomCell *customCell = [collectionView dequeueReusableCellWithReuseIdentifier:#"customCell" forIndexPath:indexPath];
return customCell;
}
else {
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"photo" forIndexPath:indexPath];
_obj_IndexPath = indexPath;
NSLog(#"%ld",(long)_obj_IndexPath.row);
_imageView = (UIImageView *)[cell viewWithTag:200];
_imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
return cell;
}
}
The goal is a normal UICollectionView functionality with "full-screen" cells that scroll vertically AND during a certain condition scrolling to the previous cell is limited to only one scroll back AND that cell has a special / custom view on top of the cell content.
Any help or ideas would be awesome. Thank you!
I've updated my current code per comments below

How to save state of UICollectionViewCell

Please help me, how can I save state of UICollectionViewCell. For example: I have cells with UIWebView and when I dequeueReusableCellWithReuseIdentifier:forIndexPath: returned wrong cell, not for this indexPath and after it UIWebView content will reloaded for correct indexPath with twitches. Thanks!
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NewsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"NEWS_CELL" forIndexPath:indexPath];
if (!cell.isShowed) { // I just added boolean property
cell.isShowed = YES;
/*** some init actions ***/
}
return cell;
}
If I use this method, cells with cell.isShowed == YES return on wrong indexPath. But if I not use cell.isShowed content of UIWebView will reloaded in every showing cell
Sorry for my english :)
See what Apple's UIWebView Class Reference say:
Important: You should not embed UIWebView or UITableView objects in
UIScrollView objects. If you do so, unexpected behavior can result
because touch events for the two objects can be mixed up and wrongly
handled.
But UICollectionView inherits from UIScrollView. So you need to redesign your architecture.