【发布时间】:2017-01-14 15:07:57
【问题描述】:
我的应用程序包含一个 plist 文件,它是我的 UICollectionView 数据。 plist 如下所示:
这是我的集合视图代码:
-(NSArray *)content {
if (!_content) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"myJournal.plist"];
NSDictionary *help = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
_content = [help allValues];
_titles = [help allKeys];
NSLog(@"CONTENT%@", _content);
}
return _content;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.content count];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"MM/dd/yyyy";
NSDate *date = [dateFormatter dateFromString:[self.titles objectAtIndex:indexPath.row]];
dateFormatter.dateFormat=@"MMMM";
NSString *monthString = [[dateFormatter stringFromDate:date] capitalizedString];
dateFormatter.dateFormat=@"dd";
NSString *dayString = [dateFormatter stringFromDate:date];
static NSString *cellIdentifier = @"cvCell";
CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[cell.labels setText:[[self.content objectAtIndex:indexPath.row] valueForKey:@"verse"]];
[cell.month setText:monthString];
[cell.day setText:dayString];
return cell;
}
以下是视图的外观: 正如你所知道的,这不是我想要的布局方式。我更喜欢从左到右和从上到下的顺序,或者最新的条目优先,但我不能完全明白。关于如何实现这一点的任何建议?
更新:
我添加了一个 NSArray 并使用它首先从 plist 中获取数据,然后使用一个 NSSortDescriptor 来创建 _content 数组。在 NSLog 中,它按我想要的顺序显示,但最终结果没有改变:
-(NSArray *)content {
if (!_content) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *plistPath = [[paths lastObject] stringByAppendingPathComponent:@"myJournal.plist"];
NSDictionary *help = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
_sortedArray = [help allValues];
_titles = [help allKeys];
NSSortDescriptor* nameSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"theDate" ascending:NO];
_content = [_sortedArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:nameSortDescriptor]];
NSLog(@"%@", _content);
}
return _content;
}
【问题讨论】:
标签: ios objective-c uicollectionview nsarray