1
votes

I am creating a UICollectionView and I am facing an issue when a cell is selected, I think the function didSelectItemAtIndexPath is not being called.

part of my code is: viewDidLoad

[self.collectionCategories registerNib:[UINib nibWithNibName:@"CTMMenuCategoryCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
self.collectionCategories.allowsMultipleSelection = NO;
self.collectionCategories.delegate = self;
self.collectionCategories.dataSource = self;

Delegate methods:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return categoriesArray.count;
    [self.collectionCategories reloadData];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CTMMenuCategoryCell *cell = (CTMMenuCategoryCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CTMMenuCategoryCell alloc] init];
    }

    CTMCategory *categoria = [categoriesArray objectAtIndex:indexPath.row];
    cell.categoryName.text = categoria.name;    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize calCulateSizze = [(NSString*)[[categoriesArray objectAtIndex:indexPath.row] valueForKey:@"name"] sizeWithAttributes:NULL];
    calCulateSizze.height = 64;
    calCulateSizze.width = calCulateSizze.width+80;
    return calCulateSizze;
}

- (void)tableView:(UICollectionView *)tableView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CTMMenuCategoryCell *cell = (CTMMenuCategoryCell *)[self.collectionCategories cellForItemAtIndexPath:indexPath];
    cell.categoryName.textColor = [UIColor redColor];
}

The clases for the custom Cell (it has also a xib file):

// .h

#import <UIKit/UIKit.h>
@interface CTMMenuCategoryCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UILabel *categoryName;

@end


//.m
#import "CTMMenuCategoryCell.h"

@implementation CTMMenuCategoryCell

- (void)awakeFromNib {
    self.categoryName.textColor = [UIColor whiteColor];
    [self.categoryName.superview sizeToFit];
}

- (void)prepareForReuse
{
    [super prepareForReuse];
    self.categoryName.text = nil;
}

@end

I need to change the property textColor of the label, I hope you can help me with this, I don't know if I am missing a method or if I am doing something wrong.

Thanks

2
Are you using any button in CTMMenuCategoryCell which cover almost whole area?Tapas Pal
no, the cell only has a text label. Should I use buttons?antonio__
confirm the protocolsvijeesh
No no, you don't need to use buttons. Which protocols you have confirm?Tapas Pal
@antonio__ Please try to debug and see your delegate method called or not.Ashok Londhe

2 Answers

2
votes

You have created collection view and have implemented the delegates for same, but you have implemented the delegate method didSelectItemAtIndexPath on table view

So, you need to change the

- (void)tableView:(UICollectionView *)tableView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CTMMenuCategoryCell *cell = (CTMMenuCategoryCell *)[self.collectionCategories cellForItemAtIndexPath:indexPath];
    cell.categoryName.textColor = [UIColor redColor];
}

to

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
{
    CTMMenuCategoryCell *cell = (CTMMenuCategoryCell *)[self.collectionCategories cellForItemAtIndexPath:indexPath];
    cell.categoryName.textColor = [UIColor redColor];
}
1
votes

The UICollectionViewDelegate protocol defines methods that allow you to manage the selection and highlighting of items in a collection view and to perform actions on those items.

You need to confirm UICollectionViewDelegate protocol to use didSelectItemAtIndexPath method