gpt4 book ai didi

ios - 自定义 Collection View 单元格的重写方法不调用

转载 作者:行者123 更新时间:2023-11-28 21:32:56 24 4
gpt4 key购买 nike

我有一个自定义 Collection View 单元格:BaseCollectionViewCell,我扩展了该类并产生了PastMonth CurrentMonth FutureMonth子类。但是,当我运行该应用程序时,他们假定的覆盖方法 -setView- 不起作用。

这是委托(delegate)方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == self.currentIndex && self.currentIndex != 0) {
AnotherExampleCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AnotherExampleCollectionViewCell" forIndexPath:indexPath];
return cell;
}
int typeOfMonth = [self findTypeOfMonth:(int)indexPath.row birthDate:self.birthDate];
switch (typeOfMonth) {
case 0: {
PastMonth* cell = (PastMonth*)[collectionView dequeueReusableCellWithReuseIdentifier:@"BaseCollectionViewCell" forIndexPath:indexPath];
[cell setView];
return cell;
}
case 1:{
CurrentMonth* cell = (CurrentMonth*)[collectionView dequeueReusableCellWithReuseIdentifier:@"BaseCollectionViewCell" forIndexPath:indexPath];
[cell setView];
return cell;
}
default:{
FutureMonth* cell = (FutureMonth*)[collectionView dequeueReusableCellWithReuseIdentifier:@"BaseCollectionViewCell" forIndexPath:indexPath];
[cell setView];
return cell;
}
}

这是 BaseCollectionViewCell.h 和 .m:

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

@property (strong, nonatomic) IBOutlet UIView *coverView;

-(void) setView;
@end



#import "BaseCollectionViewCell.h"

@implementation BaseCollectionViewCell

- (void)awakeFromNib {
// Initialization code
}

-(void) setView{
NSLog(@"asdasd");
}
@end

这是 CurrentMonth.m:(我没有放其他文件,因为它们几乎相同):

#import "BaseCollectionViewCell.h"
@interface CurrentMonth : BaseCollectionViewCell
-(void) setView;
@end

#import "CurrentMonth.h"

@implementation CurrentMonth

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(void) setView{
self.coverView.hidden = YES;
[self.contentView setBackgroundColor:[UIColor yellowColor]];
}
@end

例如,为什么不调用 CurrentMonth 的 setView 而调用 BaseCollectionViewCell 的 setView 方法?

谢谢 :)

最佳答案

您必须使用 registerClass 为每个单元格标识符注册正确的类

[self.collectionView registerClass:[CurrentMonth class] forCellWithReuseIdentifier:@"CurrentMonthCollectionViewCell"];

与其他类(class)类似,您需要这样做。

来自 Apple 文档:

You typically do not create instances of this class yourself. Instead, you register your specific cell subclass (or a nib file containing a configured instance of your class) with the collection view object. When you want a new instance of your cell class, call the dequeueReusableCellWithReuseIdentifier:forIndexPath: method of the collection view object to retrieve one.

你确定你正在这样做吗?

或者,如果您的单元格位于 xib 中,则必须为 xib 中的单元格设置适当的自定义类

关于ios - 自定义 Collection View 单元格的重写方法不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35148872/

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