【发布时间】:2015-04-17 10:25:01
【问题描述】:
编辑:我应该指定这仅在我尝试使用 UICollectionViewFlowLayout 时发生,而不是在我尝试使用自定义视图时发生。但无论哪种方式,尽管在我从 TableView 转换之前它工作得很好,但 CollectionView 上没有任何显示。)
所以我一直在尝试将我拥有的 UITableView 转换为 UICollectionView。到现在为止还挺好。但是当我尝试运行该应用程序时,它给了我错误:
'集合视图的数据源没有从 -collectionView:cellForItemAtIndexPath: for index path {length = 2, path = 0 - 0} 返回有效单元格'
我在这里检查了所有类似的问题和答案......所以在我的 viewDidLoad 中我有(tableView 实际上是一个 UICollectionView):
UINib * placeCell = [UINib nibWithNibName:@"Shops" bundle:nil];
[self.tableView registerNib:placeCell
forCellWithReuseIdentifier:CellIdentifier];
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UICollectionView *)tableView numberOfItemsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_entries count];
//return 5;
}
- (void)tableView:(UICollectionView *)tableView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item == [_entries count]-1 && page > 1) {
NSLog(@"load more");
//add footer view loading
if (c_page == page) {
// _tableView.tableFooterView = nil;
}
else
{
c_page++;
[self loadPlace:c_page];
}
}
}
- (UICollectionViewCell *)tableView:(UICollectionView *)tableView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
PlaceCell *cell = (PlaceCell *)[tableView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UINib * placeCell = [UINib nibWithNibName:@"Shops" bundle:nil];
//cell = [cellLoader instantiateWithOwner:self options:nil];
NSArray *topLevelItems = [placeCell instantiateWithOwner:self options:nil];
cell = [topLevelItems objectAtIndex:0];
Place *p = [_entries objectAtIndex:indexPath.row];
cell.placeName.text = p.PName;
NSLog (@"p:%@", p.PName")
cell.placeImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:p.PImage]]];
return cell;
}
我进入 UICollectionViewCell (PlaceCell) 的 xib 并确保“Cell”是重用标识符。我确保数据源和委托已连接到 collectionView 中的文件所有者。
我还注意到,当我使用自定义布局而不是流布局时(比如这个:https://github.com/ShadoFlameX/PhotoCollectionView/blob/master/CollectionViewTutorial/BHPhotoAlbumLayout.m),它并没有给我这个错误......但我的 collectionview 仍然没有填充。
所以我想知道是否有某种日志可以运行,或者我可以做些什么来找出问题所在。因为我已经尝试了所有我见过的解决方案,但都没有成功。
【问题讨论】:
标签: xcode layout uicollectionview