我正在尝试将静态数据添加到UiCollectionViewController中。 问题是程序启动后它给了我未捕获的异常 错误日志:
2014-03-27 19:13:35.519 Rss[5435:a0b] viewDidLoad
2014-03-27 19:13:35.521 Rss[5435:a0b] numberofsectionsInCOllectionView
2014-03-27 19:13:35.521 Rss[5435:a0b] numberOfItemInSection
2014-03-27 19:13:35.523 Rss[5435:a0b] *** Assertion failure in -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit_Sim/UIKit-2903.23/UICollectionView.m:1365
2014-03-27 19:13:35.530 Rss[5435:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path <NSIndexPath: 0xa02eae0> {length = 2, path = 0 - 0}'
这是我的CollectionView服装类:
#import "RootViewController.h"
#define kCustomRowCount 7
@interface RootViewController ()
@property (nonatomic, strong) NSMutableDictionary *imageDownloadsInProgress;
@end
@implementation RootViewController{
NSMutableArray *array;
}
#pragma mark
// -------------------------------------------------------------------------------
// viewDidLoad
// -------------------------------------------------------------------------------
- (void)viewDidLoad
{
NSLog(@"viewDidLoad");
[super viewDidLoad];
array = [[NSMutableArray alloc] init];
[array addObject:@"User1"];
[array addObject:@"User2"];
[array addObject:@"User3"];
[array addObject:@"User4"];
[array addObject:@"User5"];
[array addObject:@"User6"];
[array addObject:@"User7"];
[array addObject:@"User8"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
NSArray *allDownloads = [self.imageDownloadsInProgress allValues];
[allDownloads makeObjectsPerformSelector:@selector(cancelDownload)];
[self.imageDownloadsInProgress removeAllObjects];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
NSLog(@"numberofsectionsInCOllectionView");
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(@"numberOfItemInSection");
return [array count];
}
}
- (UICollectionViewCell *)collectionview:(UICollectionView *)collectionview cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForItemAtIndexPath");
UICollectionViewCell *cell = [collectionview dequeueReusableCellWithReuseIdentifier:@"CELL"
forIndexPath:indexPath ];
UILabel *label = (UILabel *)[cell viewWithTag:100];
label.text = [array objectAtIndex:indexPath.row];
return cell;
}
@end
我用Google搜索了错误,但此行没有出现任何内容:“数据源未返回有效单元格”。 我通过拖动它们将故事板中的数据源和委托设置为类。