2013-09-22 37 views
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]; 
} 

回答

1

这似乎是一个缓存或IOS 7的升级问题。我完全卸载了应用程序,重新启动了xcode和模拟器,问题现在消失了。