didSelectItemAtIndexPath方法不在UICollectionView中工作

时间:2015-04-28 07:12:26

标签: ios objective-c

我正在创建一个UICollectionView,当我选择一个单元格时,我遇到了一个问题,我认为函数didSelectItemAtIndexPath没有被调用。

我的部分代码是: 的 viewDidLoad中

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

委托方法:

- (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];
}

自定义Cell的clases(它还有一个xib文件):

// .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

我需要更改标签的属性textColor,希望你能帮我解决这个问题,我不知道我是否错过了某种方法,或者我是否做错了。

谢谢

2 个答案:

答案 0 :(得分:2)

您已创建了集合视图并已为其实现了委托,但您已在表视图上实现了委托方法didSelectItemAtIndexPath

所以,你需要改变

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

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

答案 1 :(得分:1)

UICollectionViewDelegate协议定义了一些方法,允许您管理集合视图中项目的选择和突出显示,并对这些项目执行操作。

您需要确认UICollectionViewDelegate协议才能使用didSelectItemAtIndexPath方法