how to know if UICollectionViewCell is about to be dequed? - ios

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

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;

iOS cellForItemAtIndexPath beyond the Array bound

A question about UICollectionView. UICollectionView nested in UITableViewCell. The problem is the following simple code, but online [self.imageInfos objectAtIndex:indexPath.row] here often appear array beyond bound, very strange!! IndexPath bound is not above the collectionView: (UICollectionView *) collectionView numberOfItemsInSection: (NSInteger) section this function constraints, why the array beyond bound.
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return self.imageInfos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
LKMImageInfo *imageInfo = [self.imageInfos objectAtIndex:indexPath.row];
return imageCell;
}
Your UICollectionView item represents LKMImageInfo object. So instead of fetching LKMImageInfo object from indexPath.row, you should fetch it from indexPath.item. Like below:
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return self.imageInfos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
LKMImageInfo *imageInfo = [self.imageInfos objectAtIndex:indexPath.item];
return imageCell;
}
Use custom table view cell and inside add collection view. Write collection view delegates in the custom class of table view cell.

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.

collectionView: didSelectItemAtIndexPath: does not get called

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.

Compare Reuse Identifier Crashing

I need to have a custom cell height for all of my cells in my UITableView. In this method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
I try this:
if ([[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] == #"imageCell")
As I have 3 different cells setup with different identifiers in my storyboard. However my app just crashes here with EXC_BAD_ACCESS.
Any idea why?
Thanks.
You're comparing a string, so you should be using isEqualToString:
if ([[[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] isEqualToString:#"imageCell"])
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
you are supposed to calculate the height for you row, not to get the Cell of your tableView.
In my opinion, in the lifeCycle of a TableView delegate, the 1st step (before allocating each UITableViewCell), the TableView delegate call heightForRowAtIndexPath for each row (but at this moment, the UItableViewCell are not allocated). The, in a 2nd step, the TableView call
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
to create UitableView Cells.

Resources