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
CTMMenuCategoryCell
which cover almost whole area? – Tapas Pal