1

爲了序言,我有一些iOS開發經驗,但這是我第一次發佈到AppStore的應用程序,也是我第一次使用UICollectionView的經驗。UICollectionViewCell的實例顯示在UICollectionView的其他實例後面

這裏是我在我的應用程序中做什麼的總結: 我有一個分頁的UICollectionView,其中包含幾個全屏UICollectionViewCells。在每個這些單元格中都有另一個包含UICollectionViewCells('subCollectionViewCells')的UICollectionView('subCollectionView'),它們的標籤目前被設置爲「測試文本」。當滾動到第二個collectionViewCell時,第一個collectionViewCell的subCollectionViewCells在第二個collectionViewCell的subCollectionView後面可見。我已將圖像鏈接到發生的事情here

的CollectionViewCell包含subCollectionView:

[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 
+0

您是否找到了解決方案或者您是否嘗試了第一個答案? – Hugo

+0

最終成爲解決問題的唯一方法是將collectionView的滾動位置重置爲索引0處的項目。當用戶移動到pageViewController中的不同頁面時,它不保留collectionView的滾動位置,但通過將視圖重置爲開始滾動位置並重新繪製視圖,我能夠僞造它。 – spedga

回答

0

我有一個類似的問題與標籤覆蓋自己的CollectionView,嘗試調用awakeFromNib你updateCell方法,這並獲得成功對我來說。