I have a tableview, in this tableview i have four rows, in each row i added a collection view programatically and i check that if its my first row i should register 1st nib of collection view cell, if its my 2nd row, i should register 2nd nib of collection view cell and so on and than i simply reloads the collection view. I used the below code:-
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cellID"];
if(cell==nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellID"];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
collectionView.dataSource=self;
collectionView.delegate=self;
if (indexPath.row==0) {
[collectionView registerNib:[UINib nibWithNibName:#"QuotesCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"quotes"];
} else if (indexPath.row==1){
[collectionView registerNib:[UINib nibWithNibName:#"AlphabeticalWiseCategoriresCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"alphabets"];
} else if (indexPath.row==2){
[collectionView registerNib:[UINib nibWithNibName:#"DaysCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"days"];
}else if(indexPath.row==3){
[collectionView registerNib:[UINib nibWithNibName:#"LifeAspectsCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"life"];
}
[collectionView reloadData];
return cell;
}
My problem/question is how could i come to know with which collection view nib i have to deal with in following delegates, because here i have to specify the same reuse identifier that i have used while registering nib with collection view ?
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
}
My Second problem is how could i detect which tableview row's collection view cell's is clicked since i have different collection view?
Firstly, why do you not use static table view? If you hardcode every cell, I think it will be good enhancement.
Next about your problem, iOS provides every UIView tag property.
If you remake your table to static table, then you will can set tag property via InterfaceBuilder. If not:
UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
collectionView.dataSource=self;
collectionView.delegate=self;
collectionView.tag = indexPath.row;
Then:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
switch collectionView.tag {
case 0:
...
break
case 1:
...
break
}
}
And your can use this approach to resolve your second problem
You could keep the collectionView you created each time, say, in an array. So the index of each collectionView in the array will be equal to the indexPath.row. By retrieve the index of the collectionView parameter in - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath, you could know which collectionView you are dealing with, as well as the UICollectionViewCell.
Related
I have a UICollectionView and I add from my cellForItemAtIndexPath 12 cells. When I do scroll on this to see down cells, all functions is preserved, but when I do scroll to go up again, some cells don't execute the function loaded in didSelectItemAtIndexPath.
I set disabled some rows. But not the row that I clicked Why could be this? Could be a bad prepare for reuse cell?
I'm trying the reuse function, but this only affect drawing the cell wrong or on the another position and the function added in didSelectItemAtIndexPath not work:
[self.myCollectionView reloadData];
[self.myCollectionView layoutIfNeeded];
NSArray *visibleItems = [self.myCollectionView indexPathsForVisibleItems];
NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[self.myCollectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
WHen I do scroll and the do click to one cell, this not open my secondViewController, I think that this cell is gettin a wrong index that was disabled.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"myCell" forIndexPath:indexPath];
switch ([indexPath row]) {
case 0:
titleCell= #"Title0";
detailCell=#"Detail0";
if([indexPath row]==2){
[cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}
[cell.image setHidden:YES];
cell.image.contentMode = UIViewContentModeScaleAspectFit;
break;
//Here other cases with the same structure
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[collectionView deselectItemAtIndexPath:indexPath animated:NO];
MySecondClass *secondClass = [self.storyboard instantiateViewControllerWithIdentifier:#"myVC"];
[self.navigationController pushViewController:secondClass animated:YES];
}
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
//Here assign yellow background color to first row and white color to the second row
}
And in my cell class I added the prepareForReuse but this not work...
- (void)prepareForReuse {
[super prepareForReuse];
[self removeFromSuperview];
[self setNeedsLayout];
}
The problem lies on this piece of code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyViewController *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"myCell" forIndexPath:indexPath];
switch ([indexPath row]) {
case 0:
titleCell= #"Title0";
detailCell=#"Detail0";
if([indexPath row]==2){
[cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}
[cell.image setHidden:YES];
cell.image.contentMode = UIViewContentModeScaleAspectFit;
break;
return cell;
}
Because cells are reusable. So the cell that you disabled earlier (For example cell0) will continue to be reused. When you scroll, that cell0 will become cell11 for example. However, its setting is still disabled.
You need to add a else statement to remove the disabled setting like this.
if([indexPath row]==2){
[cell setUserInteractionEnabled:NO];//Here I set disable Could be the problem caused by this??
}
else{
[cell setUserInteractionEnabled:YES];
}
When a cell scrolls off the screen it is potentially deallocated or (more likely) recycled, so any state you have in that cell is not persisted once its off screen. The solution is that you should check if a cell is selected and update its appearance in cellForRowAtIndexPath. You can just have didSelectItemAtIndexPath and didDeselectItemAtIndexPath call reloadRowsAtIndexPath and cellForRowAtIndexPath will handle all of the highlighting appearance logic in one place.
All,
I am adding a UICollectionView to a UIView but it's failing to load cells because it seems that this:
[self.buttonsCollectionView registerNib:[UINib nibWithNibName:#"AccountMenuCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"AccountMenuCell"];
Is getting loaded after it hits the delegates for the UICollectionView. Normally this would be placed in viewDidLoad but is not available in a UIView.
- (AccountMenuCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AccountMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"AccountMenuCell" forIndexPath:indexPath];
[cell.accountMenuButton setTitle:#"Deposit" forState:UIControlStateNormal];
return cell;
}
Any guidance?
EDIT:
So I am using a view from the storyboard initially and then adding a subview which is Account Menu. Within this view I have a Collection View.
Try to use with the default method and instantiate your Cell inside of the cellForItem...
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AccountMenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"AccountMenuCell" forIndexPath:indexPath];
[cell.accountMenuButton setTitle:#"Deposit" forState:UIControlStateNormal];
return cell;
}
and .. verify with Breakpoints if your code is running..
I'm missing something obvious here as this isn't a difficult task.
I have my Collection view in storyboard (amongst other views), with the prototype cell's reuse id being "Cell", and a UILabel in that cell, with a tag of 100.The code (boilerplate):
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 4;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:100];
label.text = #"Hello";
return cell;
}
The code is within a UICollectionViewController, as it should.
When i run, the view appears yet no cells or text are visible.
A NSLog in the cellForItemAtIndexPath confirms that it is called, 4 times.
I've even changed the prototype cell's background color which shows that not even the cell's are appearing.
In the viewDidLoad, it calls: [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
What am i missing?
Use like this:
Instead of registerClass: use registerNib:
static NSString *identifier = #"Cell";
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
[self.collection registerNib:[UINib nibWithNibName:#"CollectionPhotoItem" bundle:nil] forCellWithReuseIdentifier:identifier];
}
else{
[self.collection registerNib:[UINib nibWithNibName:#"CollectionPhotoItem_ipad" bundle:nil] forCellWithReuseIdentifier:identifier];
}
make sure you are under any-any ratio in your storyboard
if you are working with any other configuration and you tried to use the simulation it might not show up
for example if you use regular width and compact hight and then tried to use the simulator in your Xcode all your work will not show up !
I am using collectionView in my App. I am setting image for the cell backgroundView in didSelect delegate. But When i select one cell indexPath the image is getting set for 3 cell indexPath. When i scroll the collectionView the images are getting changed randomly? Please Help me. thanks in advance.
- (void)viewDidLoad
{
[super viewDidLoad];
[collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:uio];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return 50;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio
forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"index %#",indexPath);
UICollectionViewCell *cell = [collection cellForItemAtIndexPath:indexPath];
cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"download.jpg"]];
}
That's because you reuse your cell. An option would be to have an dictionary variable to say that your cell has been selected and reset the image if it has not been.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"index %#",indexPath);
UICollectionViewCell *cell = [collection cellForItemAtIndexPath:indexPath];
cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"download.jpg"]];
[selectedDictionary setObject:[NSNumber numberWithBool:YES] forKey:[NSNumber numberWithInteger:indexPath.row]];
}
Then in your cellForItemAtIndexPath method you would check that value
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio
forIndexPath:indexPath];
BOOL selected = [[selectedDictionary objectForKey:[NSNumber numberWithInteger:indexPath.row]] boolValue];
if(selected){
cell.backgroundView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"download.jpg"]];
}else{
cell.backgroundView = nil;
}
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
Of course if you use some kind of object as model, it would appropriate to have a selected variable in here, you won't need a nsdictionary any more.
The Problem is dequeueReusableCellWithReuseIdentifier.
When you scroll UICollectionview then cell are reused that is problem
add Collectionview inside scrollview.
Try this Inside:
Scroll_View is Your Scroll View
collection is Your Collectionview
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
self.Scroll_View.contentSize = CGSizeMake(self.view.frame.size.width, collectionView.contentSize.height);
CGRect fram_For_Collection_View = self.collection_view.frame;
fram_For_Collection_View.size.height = collectionView.contentSize.height;
self.collection.view.frame = fram_For_Collection_View;
}
Your -collectionView:didSelectItemAtPath: is adding a new image view to the cell. Nothing is removing that image view when the cell is reused. So, when you say:
UICollectionViewCell *cell = [collection dequeueReusableCellWithReuseIdentifier:uio
forIndexPath:indexPath];
in your -collectionView:cellForItemAtIndexPath:, you're may get back some cell that already has one or more image views.
My suggestion would be to add the image view to the cell in the cell prototype, perhaps in your storyboard or in the cell's initializer. Have your -collectionView:cellForItemAtIndexPath: set the image for that image view to the correct image for the given path.
What's happening is that UICollectionView reuses cells. So in didSelectItemAtIndexPath: you set the cell background, but then the UICollectionView reuses that same cell as needed (and you're not resetting the cell.backgroundView in cellForItemAtIndexPath:).
The way to fix this is to maintain an NSIndexSet of selected cells. In didSelectItemAtIndexPath: you can add the index of the item that was selected, and then force a reload of that item by calling reloadItemsAtIndexPaths. Then, in your cellForItemAtIndexPath: check the index set to see if the selected index is included, and if so, set the backgroundView of the cell.
I had the same issue few days ago & I posted a question here. Here is the answer I got & it works for me.
Collection View Cell multiple item select Error
And also if you are using a custom cell you can add this code to the init method of that cell & it will work too.
CGFloat borderWidth = 6.0f;
UIView *bgView = [[UIView alloc] initWithFrame:frame];
bgView.layer.borderColor = [UIColor redColor].CGColor;
bgView.layer.borderWidth = borderWidth;
self.selectedBackgroundView = bgView;
I have a collection view in my app, I want it to contain a custom cells. I've created a custom cell view xib file. Then I use it in my data source method :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath];
if(cell == nil){
NSArray *nsObjects = [[NSBundle mainBundle] loadNibNamed:#"OtherCustomersCell" owner:nil options:nil];
for(id obj in nsObjects)
if([obj isKindOfClass:[OtherCustomersCell class]])
cell = (OtherCustomersCell*) obj;
}
[cell.name setText:#"AAAA BBBBB"];
return cell;
}
But when I run the app, there is just a black rectangular where the collection view should be (at the bottom under the table view):
What am I doing wrong?
Thank you in advance.
Collection views work differently than table views in that you don't have to create a cell if one can't be dequeued.
Instead, you have to register the nib for the cell first:
- (void)viewDidLoad
{
...
UINib *cellNib = [UINib nibWithNibName:#"OtherCustomersCell" bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER];
}
You can then dequeue the cell and it will be created automatically for you if necessary:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
OtherCustomersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:OTHER_CUSTOMERS_CELL_IDENTIFIER forIndexPath:indexPath]; // cell won't be nil, it's created for you if necessary!
[cell.name setText:#"AAAA BBBBB"];
return cell;
}
You have to register UICollectionView instance in the following way in viewdidload then you can use this.
[self.photoListView registerNib:[UINib nibWithNibName:#"UIcollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"Identifier"];