1
後消失在我的應用程序中,我建立了一個基於數據集的集合視圖。當我執行全新安裝時,所有標題標題顯示正確。重新打開應用程序後,第一個標題標題消失。並且在viewWillAppear方法中調用reloadData並沒有解決問題。UICollectionView標題標題在
我的代碼:
#import "ChapterViewController.h"
#import "XMLParser.h"
#import "ChapterStore.h"
#import "Chapter.h"
#import "Section.h"
#import "Word.h"
#import "ChapterViewCell.h"
@interface ChapterViewController()
@property (nonatomic, strong) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataArray;
@end
@implementation ChapterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[self navigationItem] setTitle:@"Chapters"];
self.navigationController.navigationBar.translucent = NO;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (![userDefaults valueForKey:@"version"])
{
[[ChapterStore sharedStore] doParse];
[self spaceTrimmer:[ChapterStore sharedStore]];
// Adding version number to NSUserDefaults for first version:
[userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
}
if ([[NSUserDefaults standardUserDefaults] floatForKey:@"version"] == [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue])
{
/// Same Version so dont run the function
}
else
{
// Call Your Function;
// Update version number to NSUserDefaults for other versions:
[userDefaults setFloat:[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] floatValue] forKey:@"version"];
}
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[[self collectionView] reloadData];
}
- (NSInteger)numberOfChapters {
return [[[ChapterStore sharedStore] allChapters] count];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *chapters = [[NSMutableArray alloc] init];
for (int i = 0; i < [self numberOfChapters]; i++) {
[chapters addObject:[[[ChapterStore sharedStore] allChapters] objectAtIndex:i]];
}
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
self.dataArray = chapters;
UINib *cellNib = [UINib nibWithNibName:@"ChapterViewCell" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];
[self.collectionView registerNib:[UINib nibWithNibName:@"ChapterViewHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cvHeader"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(159, 159)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[flowLayout setMinimumInteritemSpacing:2];
[flowLayout setMinimumLineSpacing:2];
[flowLayout setHeaderReferenceSize:CGSizeMake(0, 50)];
[self.collectionView setCollectionViewLayout:flowLayout];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"cvHeader" forIndexPath:indexPath];
Chapter *data = [self.dataArray objectAtIndex:indexPath.section];
NSString *chapterName = [data CHAPTER_NAME];
UILabel *titleLabel = (UILabel *)[header viewWithTag:99];
[titleLabel setText:chapterName];
return header;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
Chapter *data = [self.dataArray objectAtIndex:indexPath.section];
Section *section = [[data sections]objectAtIndex:indexPath.row];
NSString *cellData = [section SECTION_NAME];
static NSString *cellIdentifier = @"cvCell";
ChapterViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
int i = [indexPath indexAtPosition:0] + 1;
int j = [indexPath indexAtPosition:1] + 1;
NSString *imageName = [NSString stringWithFormat:@"c%ds%d.jpg", i,j];
UIImage *image = [UIImage imageNamed:imageName];
[cell setController:self];
[cell setCollectionView:collectionView];
[[cell sectionLabel] setText:cellData];
[[cell imageView]setImage:image];
return cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return [self.dataArray count];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
Chapter *sectionArray = [self.dataArray objectAtIndex:section];
return [[sectionArray sections] count];
}