UICollectionViewCell的实例显示在UICollectionView的其他实例后面

时间:2013-10-02 19:49:50

标签: ios objective-c nested uicollectionview uicollectionviewcell

前言,我有一些iOS开发经验,但这是我要发布到AppStore的第一个应用程序,也是我第一次使用UICollectionView的经验。

以下是我在我的应用中做的总结: 我有一个分页的UICollectionView,它包含几个全屏UICollectionViewCells。在每个单元格中是另一个UICollectionView('subCollectionView'),包含UICollectionViewCells('subCollectionViewCells'),它们的标签设置为“Test Text”。滚动到第二个collectionViewCell时,第一个collectionViewCell的subCollectionViewCells在第二个collectionViewCell的subCollectionView后面可见。我已将图像链接到正在发生的事情here

包含subCollectionView的CollectionViewCell:

[VLCategoryCollectionViewCell.h]

#import <UIKit/UIKit.h>

@interface VLCategoryCollectionViewCell : UICollectionViewCell

@property (nonatomic, strong) NSString *imageName;
@property (strong, nonatomic) IBOutlet UIView *moduleSubview;

- (void)updateCell;

@end

[VLCategoryCollectionViewCell.m]

#import "VLCategoryCollectionViewCell.h"
#import "VLModuleViewController.h"

@interface VLCategoryCollectionViewCell()

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UILabel *categoryName;
@property (strong, nonatomic) VLModuleViewController *moduleViewController;

@end

@implementation VLCategoryCollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSArray *arrayOfViews = [[NSBundle mainBundle]         
loadNibNamed:@"VLCategoryCollectionViewCell" owner:self options:nil];

    if ([arrayOfViews count] < 1) {
        return nil;
    }

    if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]]) {
        return nil;
    }

    self = [arrayOfViews objectAtIndex:0];
}
return self;
}

-(void)updateCell {
_moduleViewController = [[VLModuleViewController alloc] initWithNibName:@"VLModuleViewController" bundle:nil];
_moduleViewController.view.frame = self.moduleSubview.bounds;
[self.moduleSubview addSubview:_moduleViewController.view];

NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Assets"];
NSString *filename = [NSString stringWithFormat:@"%@/%@", sourcePath, self.imageName];

UIImage *image = [UIImage imageWithContentsOfFile:filename];

NSString *fileTitle = [[filename lastPathComponent] stringByDeletingPathExtension];
[self.categoryName setFont:[UIFont fontWithName:@"OpenSans-ExtraBold" size:72.0]];
[self.categoryName setText:fileTitle];
[self.imageView setImage:image];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}

@end

[VLModuleViewController.h]

#import <UIKit/UIKit.h>

@interface VLModuleViewController : UIViewController <UICollectionViewDataSource,     UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

[VLModuleViewController.m]

#import "VLModuleViewController.h"
#import "VLModuleCollectionViewCell.h"

@interface VLModuleViewController ()

@property (nonatomic, strong) IBOutlet UICollectionView *moduleView;
@property (nonatomic, strong) NSArray *moduleArray;
@property (nonatomic) int currentIndex;
@property (nonatomic) NSString *cellIdentifier;

@end

@implementation VLModuleViewController

- (void)loadView {
[super loadView];

NSArray *moduleArray = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"Module_Tests"];
NSMutableArray *moduleImageArray = [[NSMutableArray alloc] initWithCapacity:[moduleArray count]];
for (NSString *path in moduleArray) {
    [moduleImageArray addObject:[UIImage imageWithContentsOfFile:path]];
}

self.moduleArray = [NSArray arrayWithArray:moduleArray];
}

- (void)viewDidLoad {
[super viewDidLoad];
[self setupCollectionView];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.moduleView setPagingEnabled:YES];
[self.moduleView setCollectionViewLayout:flowLayout];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

#pragma mark -
#pragma mark UICollectionView methods

- (void)setupCollectionView {
[self.moduleView registerClass:[VLModuleCollectionViewCell class] forCellWithReuseIdentifier:@"VLModuleCollectionViewCell"];

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.moduleView setCollectionViewLayout:flowLayout];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.moduleArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
VLModuleCollectionViewCell *cell = (VLModuleCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"VLModuleCollectionViewCell" forIndexPath:indexPath];
cell.titleLabel.text = @"Module Title Goes Here";
return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(235, 147);
}

@end

1 个答案:

答案 0 :(得分:0)

我有一个类似的问题,标签会在collectionView中覆盖自己,尝试在awakeFromNib中调用你的updateCell方法,这对我有用。