UICollectionView visibleCells returns nil - objective-c

Although UICollectionViewCells is displayed, [collectionView visibleCells] returns nil.
- (void)viewDidLoad{
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:#"Cell"];
[self.view addSubview:self.collectionView];
NSLog(#"%d", [[self.collectionView visibleCells] count]);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
return cell;
}
Do you have any idea?

The issue is caused by reload has not been finished.
The solution is :
1. [collectionView reloadData];
2. **[collectionView layoutIfNeeded];
3. the get your - > [collectionView visibleCells];

Try this in your view controller
- (void) viewDidLayoutSubviews {
NSLog(#"%d", [[self.collectionView visibleCells] count]);
}

Try add this:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView?.collectionViewLayout.invalidateLayout()
}

Related

Objective C Passing CGFloat value between two classes

I have a small problem ..
For convenience and to make you understand better how to share the collection View:
Collectionview 1 has the customized segment control function (it has a custom UICollectionView custom class)
Collectionview 2 contains the cells that function as pages (it has a UICollectionViewController class).
I have an UIViewController that contains the collectionview1 that I used to create a custom segment control (like a menu)
The cursor indicating which cell was selected was created with a uiview in the collectionView 1 class.
In my main view controller in addition to having a collectionview I also inserted a container view that contains a collectionview controller that will be used to show the selected pages through a horizontal scroll.
My problem now appears when I want to pass the ContentOffset value into the collectionView2 scrollViewDidScroll to the cursor in the collectionView 1.
I tried to initialize the collectionview 1 class in the collectionView 2 class to use the cursor directly but I do not get any results.
Can you help me understand how to solve this problem?
This is Class CollectionView 1 ( Menu )
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setupControl];
}
return self;
}
-(void)setupControl {
self.delegate = self;
self.dataSource = self;
self.pagingEnabled = YES;
self.showsHorizontalScrollIndicator = NO;
UINib *nib = [UINib nibWithNibName:#"UDSectionNavCell" bundle: nil];
[self registerNib:nib forCellWithReuseIdentifier:#"SCC"];
[self layout];
_horizontalCursorBar = [[UDSectionNavCursor alloc] init];
_horizontalCursorBar.frame = CGRectMake(0, self.frame.size.height - 6, self.frame.size.width / 4, 3);
_horizontalCursorBar.layer.cornerRadius = 3;
_horizontalCursorBar.backgroundColor = [UIColor colorWithHexString:#"#C1C1C1" setAlpha:1];
_horizontalCursorBar.alpha = 1;
[self addSubview:_horizontalCursorBar];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
[self selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
}
-(void)layout {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
layout.itemSize = CGSizeMake(self.frame.size.width / 4, self.frame.size.height);
layout.minimumInteritemSpacing = 10;
layout.minimumLineSpacing = 0;
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionViewLayout = layout;
[self reloadData];
}
-(NSArray *)dataPage {
return [NSArray arrayWithObjects:#"ATENEO",#"STATISTICHE",#"CALENDARIO",#"NOTIFICHE", nil];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self dataPage].count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *cellID = #"SCC";
UDSectionNavCell *cell = (UDSectionNavCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.titlePage.text = [[self dataPage] objectAtIndex:indexPath.item];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
[self scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
if (indexPath == newIndexPath) {
[self scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
[UIView animateWithDuration:.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGFloat x = (indexPath.item) * self.frame.size.width / 4;
_horizontalCursorBar.frame = CGRectMake(x, self.frame.size.height - 6, self.frame.size.width / 4, 3);
} completion:nil];
}
This is the code of CollectionView 2 (page cell)
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
UICollectionViewFlowLayout *collectionLayout = [[UICollectionViewFlowLayout alloc] init];
collectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
collectionLayout.minimumLineSpacing = 0;
self.collectionView.collectionViewLayout = collectionLayout;
[self.collectionView reloadData];
self.collectionView.pagingEnabled = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSArray *array = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor], [UIColor clearColor], [UIColor grayColor], nil];
cell.backgroundColor = array[indexPath.item];
return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"%f",scrollView.contentOffset.x / 4);
}
I would need to pass the ContentOffset value of the scrollViewDidScroll method in the collectionView 2 class and use it in the collectionView class 1

UICollectionViewCell which is reused can't be selected

When I try to tap all collectionViewCells from top to bottom, lower cells which is probably reused can't be tapped.
I guess the cells is reused with selection condition.
How do I fix it?
ViewController.m
- (void)viewDidLoad
{
flowLayout = [[UICollectionViewFlowLayout alloc]init];
flowLayout.itemSize = CGSizeMake(106, 106);
collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) collectionViewLayout:flowLayout];
collectionView.allowsMultipleSelection = YES;
[collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:kCellIdentifier];
collectionView.delegate = self;
collectionView.dataSource = self;
[self.view addSubview:collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 30;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellIdentifier forIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#", indexPath);
}
CustomCell.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
//
}
}
In -collectionView:didSelectItemAtIndexPath:, call [self.collectionView deselectItemAtIndexPath:indexPath animated:YES]; to deselect the cell so that in case it gets reused, the selection won't be passed on to be reused.
Another way of doing this is to have your model track whether it is selected or not so that you can set the selected state in -collectionView:cellForItemAtIndexPath:.

UICollectionViewCell willAppear not being called

I'm trying to do a UICollectionView of articles that doesn't eats RAM like an elephant. So, the UICollectionView consists of full screen cells that contain one image. If the UICollectionViewCell appears, the content gets loaded in. This is the code:
- (void)viewDidLoad {
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
_collectionView.pagingEnabled = YES;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
//add UIImageView here
//add UITextView with tag 123 here
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath];
UITextView *textView = [cell viewWithTag:123];
textView.text = #"Testing this out.";
}
However, this doesn't do anything. No errors are thrown, it just doesn't do anything. What am I doing wrong?

UILabel will does not show up in collectionView Cell

I have a collectionView that will display a cell from the dataArray. The dataArray is changed to self.postsArray or self.eventsArray from a segmentControl as well as the reuseIdentifier to determine the cell(a post cell and a event cell). The events work fine, but the Post Cell, which contains a label with a tag of 95, does not work. When I NSLog(#"label: %#", label); I get nil. So i have no idea what the problem is.
#pragma mark Collection View:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [dataArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (dataArray == self.postsArray) {
UILabel *label = (UILabel *)[cell viewWithTag:95];
[label.text isEqualToString:#"Hello"];
NSLog(#"label: %#", label);
return cell;
}
if (dataArray == self.eventsArray) {
PFObject *temp = [dataArray objectAtIndex:indexPath.row];
UIButton *eventButton = (UIButton *) [cell viewWithTag:10];
NSLog(#"Event: %#", eventButton);
UIButton *groupButton = (UIButton *) [cell viewWithTag:11];
[eventButton setTitle:[temp objectForKey:#"Title"] forState:UIControlStateNormal];
[groupButton setTitle:[temp objectForKey:#"Group_Name"] forState:UIControlStateNormal];
eventButton.tag = indexPath.row;
groupButton.tag = indexPath.row;
return cell;
}
return cell;
}
#pragma mark SegmentControl:
- (void) segmentChanged: (id) sender{
UISegmentedControl *seg = (UISegmentedControl *) sender;
if (seg.selectedSegmentIndex == 0) {
dataArray = self.postsArray;
reuseIdentifier = #"postsCell";
[self.collectionView reloadData];
}
if (seg.selectedSegmentIndex == 1) {
dataArray = self.eventsArray;
reuseIdentifier = #"eventCell";
[self.collectionView reloadData];
}
}
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:groupReuseIdentifier]; was left in my viewDidLoad and was hiding my first cells

multiple selection in uicollectionview not working in ios

i have multiple image in uicollectionview like grid view. i want select multiple image at a time but not working this code.please any one give idea about this code.
i have already tried this code but not working.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath];
if (self.selectedItemIndexPath)
{
// if we had a previously selected cell
if ([indexPath compare:self.selectedItemIndexPath] == NSOrderedSame)
{
// if it's the same as the one we just tapped on, then we're unselecting it
self.selectedItemIndexPath = nil;
}
else
{
// if it's different, then add that old one to our list of cells to reload, and
// save the currently selected indexPath
[indexPaths addObject:self.selectedItemIndexPath];
self.selectedItemIndexPath = indexPath;
}
}
else
{
// else, we didn't have previously selected cell, so we only need to save this indexPath for future reference
self.selectedItemIndexPath = indexPath;
}
// and now only reload only the cells that need updating
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
}
// I Have Drag the UICollectionView Controller in storyboard
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
arrImage = [[NSMutableArray alloc]initWithObjects:#"1.jpeg",#"2.jpeg",#"3.jpeg",#"4.jpeg",#"5.jpeg",#"6.jpeg",#"7.jpeg",#"8.jpeg",#"9.jpeg",#"10.jpeg",#"flower.jpeg",#"flower1.jpeg", nil];
[self.collectionView setAllowsMultipleSelection:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrImage count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
[self.view addSubview:recipeImageView];
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.15];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
}