在我的应用程序中,我基于数据集构建了一个集合视图。当我执行全新安装时,所有标题标题都显示正确。重新打开应用程序后,第一个标题标题消失。在viewWillAppear方法中调用reloadData并不能解决问题。
我的代码:
#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];
}
答案 0 :(得分:1)
这似乎是缓存或IOS 7升级问题。我完全卸载了应用程序,重新启动了xcode和模拟器,问题现在已经消失。