【问题标题】:xcode Converting UITableView to UICollectionView (no valid cell)xcode将UITableView转换为UICollectionView(无有效单元格)
【发布时间】: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


    【解决方案1】:

    当您在 xib 文件中创建单元格时,您应该注册 xib,而不是类。此外,当您注册类或 xib(或在情节提要中创建单元格)时,您不需要 if (cell==nil) 子句,因为当您使用 dequeueReusableCellWithReuseIdentifier:forIndexPath 将单元格出列时,您的单元格将永远不会为零: .你应该删除那个子句。

    【讨论】:

    • 更新了上面的代码。摆脱了“如果”条款。注册了 xib 而不是类。它甚至还没有返回细胞中的任何东西。 (有关 P.Pname 的信息,请参阅我的 NSLog)。虽然它在旧版本中使用表格视图。我注意到的一件事 Place *p = [_entries objectAtIndex:indexPath.row];我认为应该是“indexPath.item”。有很多这样的,因为它曾经是一个表格视图。所以我将它们全部切换到“indexPath.item”。不过还是没有结果……
    • [_entries count] 在 numberOfItemsInSection 中是否返回非零值?
    • 真的很难说。即使在表格视图版本中,我也无法显示下面的 NSLog ......但表格视图单元格在该版本中加载得很好。是否有一些日志或我可以在其上运行的东西来检查? if (indexPath.row == [_entries count]-1 && page > 1) { NSLog(@"load more");
    • @mysticcola 很难说是什么意思?只需在 numberOfItemsInSection 中记录 [_entries count],然后查看它的内容。
    • 我的意思是,正是那个。如果我运行 NSLog(@"Count: %d", [_entries count]);然后我什么也得不到。绝对没有什么。甚至不是“计数”。
    【解决方案2】:

    所以问题是:“从 UITableView 切换到 UICollectionView 并且没有返回有效单元格。”这实际上是一个两部分的答案。关键是 UITableView 的每一个实例...

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height-50)];
    

    ...你想变成“CollectionView”

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, self.view.bounds.size.height-50)];
    

    一切都是“行”:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    

    ...你会想变成一个“物品”。

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    

    最终我不得不完全删除以下部分:

    UINib * placeCell = [UINib nibWithNibName:@"Shops" bundle:nil];
            //cell = [cellLoader instantiateWithOwner:self options:nil];
            NSArray *topLevelItems = [placeCell instantiateWithOwner:self options:nil];
            cell = [topLevelItems objectAtIndex:0];
    

    我最好的猜测是 Nib 被加载了两次,而 Xcode 抱怨原始数据没有被加载。所以摆脱第二个条目让我的单元格加载并填充了数据。希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2018-01-29
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多