1

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

这是我在我的应用程序中所做的总结:我有一个分页的 UICollectionView,其中包含几个全屏 UICollectionViewCell。在每个单元格中都有另一个 UICollectionView ('subCollectionView'),其中包含 UICollectionViewCells ('subCollectionViewCells'),它们当前的标签设置为“测试文本”。当滚动到第二个 collectionViewCell 时,第一个 collectionViewCell 的 subCollectionViewCells 在第二个 collectionViewCell 的 subCollectionView 后面可见。我已将图像与这里发生的事情联系起来。

包含 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
4

1 回答 1

0

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

于 2014-07-05T14:50:59.793 回答