When calling -reloadData, -collectionView:didSelectItemAtIndexPath: not called - ios

When calling -reloadData several times, -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath: not called.
(When calling once or twice, the method is called correctly.)
Running -reloadData is not taking a lot of time. Memory or CPU usage is also not busy.
Why are there times when -collectionView:didSelectItemAtIndexPath: is not called?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCollectionCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"select %d", indexPath.item);
[collectionView reloadData];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"deselect %d", indexPath.item);
[collectionView reloadData];
}
CustomCollectionCell.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//
}
return self;
}

Also you have to add numberOfItemsInSection function to your code. It returns the number of items in the specified section. Without this you can't call collectionView:didSelectItemAtIndexPath function.
Also call your [collectionView reloadData]; in your viewDidLoad or viewDidAppear.
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.yourArray count];
}

Related

I cannot receive the call back in cellForItemAtIndexPath (Objective C)

I am trying to make an app with CollectionView, but I'm facing a problem. As you can see from my code, I received the callback in both numberofSectionsINCollectionView and numberOfItemsInsection, but cannot receive the callback in cellForItemAtIndexPath, which makes the cell not show up inside CollectionView. However, with another computer with the same Xcode version and connected to the same iPhone (6 Plus), I can receive the callback in cellForItemAtIndexPath.
I don't know why. Could you please tell me how to fix the problem? I think it's attributed to the setting in Xcode, but I'm not sure.
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
if (self.useSection) {
return self.dataSource.count;
}
return 1;}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.useSection)
return [[self.dataSource objectAtIndex:section] count];
return self.dataSource.count; }
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:[self reuseIdentifierForIndexPath:indexPath] forIndexPath:indexPath];
return cell;
}
I double-checked if delegate is set like
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
cellForItemAtIndexPath won't be called if your cell size is (0,0) or count is 0 maybe you can try add this code :
- (CGSize)collectionView:(UICollectionView *)collectionView layout:
(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:
(NSIndexPath *)indexPath{ return CGSizeMake(100,100);}

Odd blank space at the top of horizontal UICollectionView

I have a blank space that I can't locate the cause of at the top of a UICollectionView.
I have disabled the header in the storyboard so I don't believe it to be that.
Here is a picture:
The code for the CollectionView
- (void)setupCollectionView
{
[self.scoreCollectionView setDataSource:self];
[self.scoreCollectionView setDelegate:self];
[self.scoreCollectionView registerClass:[AAScoreCell class] forCellWithReuseIdentifier:cellIndentifier];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
AAScoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndentifier forIndexPath:indexPath];
if (_selectedIndexPath) {
[cell showSelection:[indexPath isEqual:_selectedIndexPath]];
}
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
AAScoreCell *cell = (AAScoreCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell showSelection:![indexPath isEqual:_selectedIndexPath]];
_selectedIndexPath = [indexPath isEqual:_selectedIndexPath] ? nil : indexPath;
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
AAScoreCell *cell = (AAScoreCell*)[collectionView cellForItemAtIndexPath:indexPath];
[cell showSelection:NO];
_selectedIndexPath = nil;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.scoreCollectionView.frame.size.height-50, self.scoreCollectionView.frame.size.height-50);
}
Not going to pretend I understand why but:
[self setAutomaticallyAdjustsScrollViewInsets:NO];
solved the issue for me

ios YTPlayerView does not respond to touch

I have a problem with YTPlayerView, I created a collection view, and in each cell is added subview YTPlayerView, the problem is that it shows great, but does not respond to pressing on view etc.
Not working on thumbnail page, when video playing all's fine.
How it's looking.
#pragma mark UICollectionView
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.dataSource.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath{
YTCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.identifierDataSource[indexPath.row]
forIndexPath:indexPath];
if (!cell) {
cell = [YTCollectionViewCell new];
}
if (!cell.loaded) {
[cell.playerView loadWithVideoId:self.dataSource[indexPath.row]];
cell.loaded = YES;
}
return cell;}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height);
}
-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
YTCollectionViewCell* ycell = (YTCollectionViewCell*)cell;
[ycell.playerView stopVideo];}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
YTCollectionViewCell* ycell = (YTCollectionViewCell*)cell;
if (!ycell.loaded) {
[ycell.playerView loadWithVideoId:self.dataSource[indexPath.row]];
ycell.loaded = YES;
}else{
//[ycell.playerView playVideo];
}
}
`

Does UICollectionViewCell have a viewWillAppear or something I can pass data to

I have this in my view controller:
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.dataObjects = data;
return cell;
}
In my CellSubclass.m I have:
- (id) initWithFrame:(CGRect)frame
{
// You can call initWithFrame: method here as our base constructor of the UIView super class
self = [super initWithFrame:frame];
if (self)
{
// call functions that need access to data but data is nil
}
return self;
}
So in the lifecycle of my view controller, the CellSubclass gets called immediately before the data is ready. Is there a function like viewWillAppear or something that I can pass the data too? How can I call a function from my view controller other than initWithFrame? Can I pass the data in other ways?
Thanks.
It sounds better to use your own configure method for cell in cellForItemAtIndexPath.
- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier"
forIndexPath:indexPath];
cell.dataObjects = fetchedMUtableArray[indexPath.row];
[cell somethingConfiguringMethods];
return cell;
}
UICollectionViewCell does not have something like that, but that is a subclass of UIView and that one has didMoveToSuperview.
I use it in production for some specific cases, but anyway it helps for some fast debugging also.
https://developer.apple.com/documentation/uikit/uiview/1622433-didmovetosuperview
You should return the number of cells as 0 when you do not have any data to show. When you have the data ready just reload collection view.
In your datasource methods
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
if (self.collectionViewData)
return self.collectionViewData.count;
else
return 0;
}
Lets say you have an async network call and it has returned data.
In the completion block just reload collection view.
-(void)fetchData {
[httpClient fetchDataWithCompletionBlock:^(NSArray *data) {
[self.collectionView reloadData];
}];
}

iOS: Scroll one cell at a time

I have horizontal scrolling set up now I would like for it to sort of scroll/snap one cell at a time when scrolling side to side.
Could someone shed some light? Here's the code I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 20;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:#"MyCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(50, 20, 50, 20);
}
[collectionView setPagingEnabled:YES]; might do the trick?
http://adoptioncurve.net/archives/2013/04/creating-a-paged-photo-gallery-with-a-uicollectionview/

Resources