collectionView: didSelectItemAtIndexPath: does not get called - uitableview

I have a UITableview. One of the UITableViewCell's is a UICollectionview with instance name "amenityView". The UICollectionViewDelegate and the UICollectionViewDataSource are set in the storyboard as shown below. The following methods get called and the data is populated as expected.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- (UICollectionViewCell *)collectionView:(UICollectionView *)collection cellForItemAtIndexPath:(NSIndexPath *)indexPath
However, the methods below didn't get called when I select the UICollectionViewCell contained in the UICollectionView. What have I missed?
-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
-(void) collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
UPDATE:
return YES in this method collectionView:shouldSelectItemAtIndexPath: will invoke the below two methods. At least that's what was missing on my part. Hope this will help some body...

Does the table view's didSelectRowAtIndexPath get called? If so, the table view may be intercepting the touches and not passing them through to the collection view inside. I've never done the solution you are trying to do, but the collection view is inside a scroll view, and it may not be easy to pass the touch info along to the collection view in a way that makes sense, so you may need to disable tapping on the table view cell in order for the collection view to respond to touches.

Related

UICollectionViewCell: can I know exactly when the cell appears and disappears?

I need to do some animation in a UICollectionViewCell, and I want to start the animation when the cell appears and stop/pause it when it disappears.
I guess there's no single method that tells me exactly what I want? I'm looking at:
collectionView:cellForItemAtIndexPath: I guess this isn't the place when the cell appears and visible, because it gets called even in viewDidLoad of my view controller.
layoutSubviews and didMoveToSuperview The problem is that, UICollectionView could pre-loads a cell offscreen, in this case these two won't work.
Thanks!
try using
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;
and
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

Create a collectionView from an existing tableView data source and controller

So i've been working on an ePub framework called Readium and they recently created an SDK for IOS.
The SDK is a full working project that allows a user to select an ePub from a tableView list and after selecting one gives you meta-data about that ePub after which you have to select the page list or spine items and after selecting one of the pages it finally goes to the ePub.
I want to streamline this process by opening with a list of ePubs in a collectionView and after selecting an ePub make it go straight to the first/cover page of that ePub.
What I want to know is if it is possible to use the viewController and data sources of the tableView to create a new collectionView? I have already adapted this project to allow Swipe Navigation.
It is possible to do it quite quickly.
1) You have to tell the controller that you want to adapt collection view, so you have two options in your .h file:
a) If you are currently inheriting from UITableView - inherit your ViewController from UICollectionViewController:
#interface YouViewControllerName : UICollectionViewController
b) If you inherit from UIViewController, just set your controller conforming to CollectionView delegate and dataSource methods like this
#interface YouViewControllerName : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource> { ... }
2) Then you need to replace table view delegate and data source methods with collection view data source and delegate methods, so you need to replace something like this:
Old:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ... }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { ... }
New:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath { ... }
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section { ... }
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { ... }
So the content within your previous delegate/datasource methods could remain same, as the logic is not needed to change.
You may need to implement some more specific methods and to have some work with designing right item cell sizes etc., but is is natural as your layout is replaced by collection view.

Datasource method is not called in UICollectionView

I have an UICollection with only one section.
My issue is that my datasource method :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
is never called when I init the UICollection. And I return a valid number (3) into the following datasource method !
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
After checking with breakpoints, my number of items in section 0 is returned, but still the numberOfItemsInSection method isn't called.
Any Idea ?
I precise that I've already make 6 UICollection in my app with exactly the same frame but not the same data; I don't know why, this doesn't work this time.
Ok I'm an idiot. I forgot : [self.view addSubview: myCollectionView];
Thanks for help....

CollectionView dequeCell with removing subviews from cell makes view appear slow

I have code:
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 30;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:REUSE_IDENTIFIER forIndexPath:indexPath];
[[cell subviews]
makeObjectsPerformSelector:#selector(removeFromSuperview)];
[cell addSubview:someView];
return cell;
}
Above code gets executed in viewWillAppear
If I make the above code executed 30 times, the view appearance is really slow.
Due to cell reuse, I need to make a call to removeFromSuperView. Otherwise stale wrong data is displayed on the cell.
I am not able to figure out what could be causing the slowness??
Can you provide hints?
Your appearance is slow because you are iterating through all the subviews within the cell and removing from superview. This is a very expensive process and should not be done in the cellForItemAtIndexPath method of the collectionView data source/ Infact this should never be done. If you need to display relevant content you need to access the instance of the cell and update the properties of the UI elements within the cell.

how to know if UICollectionViewCell is about to be dequed?

How can I get informed when UICollectionViewCell is about to be scroll out from superview (will be queued)? I need to resignFirstResponder from that cell before the cell is queued.
On the delegation method of UICollectionViewDelegate:
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

Resources