xcode将UITableView转换为UICollectionView(无有效单元格) [英] xcode Converting UITableView to UICollectionView (no valid cell)

查看:332
本文介绍了xcode将UITableView转换为UICollectionView(无有效单元格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该指定仅在尝试使用UICollectionViewFlowLayout时发生,而不是在尝试使用自定义视图时发生.但是无论哪种方式,尽管在从TableView转换之前,它都可以正常工作,但是CollectionView上却什么也没有显示.)

I should specify that this only happens when I attempt to use the UICollectionViewFlowLayout, not when I try to use a custom view. But either way nothing ever shows up on the CollectionView though it was working just fine before I converted from a TableView.)

因此,我一直在尝试将自己拥有的UITableView转换为UICollectionView.到目前为止,一切都很好.但是当我尝试运行该应用程序时,它给了我错误:

So I've been trying to convert a UITableView that I had into a UICollectionView. So far so good. But when I try to run the app it gives me the error:

'集合视图的数据源未从-collectionView:cellForItemAtIndexPath:返回索引路径{length = 2,path = 0-0}的有效单元格'

'the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path {length = 2, path = 0 - 0}'

我在这里检查了所有类似的问题和答案...所以在我的viewDidLoad中(tableView实际上是一个UICollectionView):

I checked all the similar questions and answers here... so in my viewDidLoad I have (tableView is actually a 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中的文件所有者.

I went into the xib of the UICollectionViewCell (PlaceCell) and made sure that "Cell" was the reuseidentifier. And I made sure that the datasource and delegate were connected to file's owner in the collectionView.

我还注意到,当我使用自定义布局而不是流程布局时(像这样: https://github.com/ShadoFlameX/PhotoCollectionView/blob/master/CollectionViewTutorial/BHPhotoAlbumLayout.m )它没有给我那个错误...但是我的collectionview仍然不是填充.

I also noticed that when I use a custom layout instead of the flow layout (like this one: https://github.com/ShadoFlameX/PhotoCollectionView/blob/master/CollectionViewTutorial/BHPhotoAlbumLayout.m ) it doesn't give me that error... but my collectionview still isn't populated.

所以我想知道是否可以运行某种日志,或者我可以做些什么来找出问题所在.因为我已经尝试了所见过的所有解决方案,但是它并没有带我到任何地方.

So I'm wondering if there's some sort of log I can run or something I can do to figure out what's going wrong. Because I've tried all the solutions I've seen and it hasn't gotten me anywhere.

推荐答案

所以问题是:从UITableView切换到UICollectionView,并且没有返回有效的单元格."这实际上是一个两部分的答案.症结所在是UITableView的每个实例...

So the problem is: "Switched from UITableView to UICollectionView and no valid cell is being returned." It is really a two part answer. The crux of which is every instance of UITableView...

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

...您想变成"CollectionView"

...you want to turn into "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

...您将想要变成一个项目".

...you'll want to turn into an "item."

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

最终我必须完全删除以下部分:

Ultimately I had to delete the following section entirely:

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抱怨原始文件没有加载数据.因此,摆脱了第二个条目,我的单元格被加载并填充了数据.希望这对某人有帮助.

My best guess is that the Nib was being loaded twice and that Xcode was complaining that the data wasn't being loaded by the original. So getting rid of that second entry got my cells loaded and populated with data. Hope this helps someone.

这篇关于xcode将UITableView转换为UICollectionView(无有效单元格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆