gpt4 book ai didi

ios - didSelectItemAtIndexPath 方法在 UICollectionView 中不起作用

转载 作者:行者123 更新时间:2023-12-01 18:52:48 25 4
gpt4 key购买 nike

我正在创建一个 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;

委托(delegate)方法:
- (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,希望您能帮我解决这个问题,我不知道我是否缺少方法或我做错了什么。

谢谢

最佳答案

您已经创建了 Collection View 并实现了相同的委托(delegate),但是您已经实现了委托(delegate)方法 didSelectItemAtIndexPath在 TableView 上

所以,你需要改变

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

关于ios - didSelectItemAtIndexPath 方法在 UICollectionView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29912664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com