繁体   English   中英

didSelectItemAtIndexPath方法不在UICollectionView中工作

[英]didSelectItemAtIndexPath method not working in a UICollectionView

我正在创建一个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的条款(它还有一个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,我希望你可以帮我这个,我不知道我是否错过了一个方法或者我做错了什么。

谢谢

您已创建了集合视图并已为其实现了委托,但您已在表视图上实现了委托方法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];
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM