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....
Related
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;
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.
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.
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.
I have a UICollectionViewController that I load when a user turns their iPad so as to put it into landscape orientation. The problem I’m having is that my UICollectionViewCells are still loading as if it were in portrait orientation. I set the UICollectionViewController to landscape inside Interface Builder, and I’m using a custom cell class that I’ve called CollectionViewCell. Each cell contains just an image and a label.
Is there something that I should be doing either in Interface Builder or in my code? I tried using CGAffineTransformationMakeRotation, but that was no help. If anyone else has encountered this before, I would be extremely grateful! Thanks very much!
Here’s a bit of the code for reference:
-(void)viewDidLoad
{
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
}
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
{
return listArray.count;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"devices" forIndexPath:indexPath];
cell.statusImage.image=[UIImage imageNamed:#"Default.png"];
cell.name.text=#"HEY !!!!!";
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(320, 420);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(50, 20, 50, 20);
}
In case anyone else has had this problem, here is how I solved it.
I was initially not allowing autorotation, and instead was just registering for orientationChanged notifications using NSNotificationCenter. This worked great except that it was preventing my second view from realizing that it needed to load in landscape mode.
So instead of that, I’ve created a class for the tabBar that my main view is embedded in, and I return NO for the shouldAutoRotate method in that class and in every other view except for the one which I want to load in landscape mode. I’m still using the orientationChanged notifications in my main view’S controller, since it is embedded in the tabBar. But I’m just using autorotation when returning to the main view.
If anyone has any questions, let me know. Hope it helps!