在 uitableviewcell 中使用 uicollectionview [英] Use uicollectionview inside uitableviewcell

查看:24
本文介绍了在 uitableviewcell 中使用 uicollectionview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决下一个问题.

I can't solve next problem.

我想显示 20 个表格单元格,每个单元格都包含一个唯一的 UICollectionView.

I want to display 20 table cells, each contains a unique UICollectionView.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *package=[_packageList objectAtIndex:indexPath.row];
        static NSString *CellIdentifier = @"package";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UICollectionView *cellImageCollection=(UICollectionView *)[cell viewWithTag:9];
        cellImageCollection.restorationIdentifier=[NSString stringWithFormat:@"%li", (long)indexPath.row, nil];

        cellTracking.text=[NSString stringWithFormat:@"Row #%li",(long)indexPath.row];
        return cell;
    }


-(NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    int row=[collectionView.restorationIdentifier intValue];
    return [[[_packages objectAtIndex:row] valueForKey:@"imageGallery"] count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    int row=[collectionView.restorationIdentifier intValue];
    NSString *imgStrLink=[[[_packages objectAtIndex:row] valueForKey:@"imageGallery"] objectAtIndex:indexPath.row];
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageID" forIndexPath:indexPath];
    UIImageView *imageView=(UIImageView *)[cell viewWithTag:2];
    imageView.image=....;
    return cell;
}

函数 tableView:cellForRowAtIndexPath: 被调用了 20 次,而 collectionView:numberOfItemsInSection:collectionView:cellForItemAtIndexPath: 只被调用了 4 次.如屏幕截图所示,UICollectionView 每四行重复一次.

Function tableView:cellForRowAtIndexPath: is called 20 times, but collectionView:numberOfItemsInSection: and collectionView:cellForItemAtIndexPath: only 4 times. As seen in the screenshots UICollectionView is repeated every fourth line.

我的错是什么?

推荐答案

我也遇到过这个问题,是因为tableview和collectionView的datasource和delegate都属于同一个父viewcontroller.

I too faced this problem, the reason I got this problem was because both datasource and delegates for the tableview and collectionView were to the same parent viewcontroller.

当我将 collectionView 的数据源和委托更改为另一个 View 时,它开始为我工作,比如 ParentView,我将 collectionView 添加到此 ParentView,然后最后将 ParentView 添加为单元格的 conentView.

It started working for me when I changed the datasource and delegate of the collectionView to another View say ParentView, and I added the collectionView to this ParentView and then finally added ParentView as conentView of the cell.

有人说我应该在此处包含内容的基本部分(因为该链接将来可能会变得无效)删除了我之前指向两个不错教程的答案...但是我不能这样做,因为这些是代码由教程所有者编写,整个代码也很重要:),所以现在我给出源代码的链接......

My previous answer which points to two nice tutorials was deleted by someone saying that I should include the essential parts of the content here(because the link may become invalid in future)...but I cannot do that as those are codes written by the tutorial owners and also the whole codes are essential too :), so now I am giving the link to the source code...

Horizo​​ntalCollectionViews

AFTabledCollectionView

-anoop

这篇关于在 uitableviewcell 中使用 uicollectionview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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