自定义集合视图单元格的重写方法不会调用

时间:2016-02-02 08:57:25

标签: ios uicollectionview uicollectionviewcell

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

这是委托方法:

- (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

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

谢谢:)

2 个答案:

答案 0 :(得分:1)

CurrentMonth* cell = (CurrentMonth*)[collectionView dequeueReusableCellWithReuseIdentifier:@"BaseCollectionViewCell" forIndexPath:indexPath];

这将返回BaseCollectionViewCell对象,这就是调用基类方法的原因。

您可能必须创建可以具有公共基类的不同自定义单元类。

switch (indexPath.row) {
    case 0: {
      PastMonth* cell = (PastMonth*)[cv dequeueReusableCellWithReuseIdentifier:@"PastMonthCollectionViewCell" forIndexPath:indexPath];
      [cell setView];
      return cell;
    }
    case 1:{
      CurrentMonth* cell = (CurrentMonth*)[cv dequeueReusableCellWithReuseIdentifier:@"CurrentMonthCollectionViewCell" forIndexPath:indexPath];
      [cell setView];
      return cell;
    }
    default:{
      FutureMonth* cell = (FutureMonth*)[cv dequeueReusableCellWithReuseIdentifier:@"FutureMonthCollectionViewCell" forIndexPath:indexPath];
      [cell setView];
      return cell;
    }
  }

然后,您可以在BaseCollectionViewCell中添加公共业务逻辑。

答案 1 :(得分:1)

您必须使用registerClass

为每个单元格标识符注册正确的类

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

同样,对于其他课程,您需要这样做。

来自Apple文档:

  

您通常不会自己创建此类的实例。代替,   您注册您的特定单元格子类(或包含   已配置的类实例)与集合视图对象。   如果需要单元类的新实例,请调用   dequeueReusableCellWithReuseIdentifier:forIndexPath:方法   集合视图对象来检索一个。

你确定要这样做吗?

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