静态 TableViewCell 中的 UICollectionView

UICollectionView inside Static TableViewCell

我在网上看到很多关于在动态 table 视图单元格中嵌入 UICollectionViews 和子类化 collection 视图以设置委托的教程,但我想知道这个过程是否与静态的有什么不同table 查看单元格。

我尝试按照示例 here 进行操作,但我无法完全理解它,因为它似乎过于复杂且几乎没有解释。有人介意概述一下我需要完成的基本步骤才能让我的 collection 视图正常工作吗?

到目前为止,这是我的代码:

class FeaturedController: UITableViewController, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    popularCollectionView.dataSource = self
    popularCollectionView.delegate = self
}

//MARK: Popular Events Collection View
    @IBOutlet weak var popularCollectionView: UICollectionView!

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = popularCollectionView.dequeueReusableCellWithReuseIdentifier("popular", forIndexPath: indexPath) as! UICollectionViewCell

        return cell
    }

非常感谢!

如果您知道如何将集合视图添加到动态 table 视图单元格,将它添加到静态视图单元格会容易得多。你根本不需要 sub class 任何东西(但这样做可能是个好主意)。在引擎盖下,静态 table 视图只不过是一个普通的 table 视图,它在托管 UITableViewController 的支持下自动设置您在 Interface Builder 中的布局。那么,方法如下:

  1. 从 Interface Builder 的对象库中拖动一个集合视图并将其放置在您想要的单元格中。
  2. 确保 Table 视图由 Table 视图控制器托管。
  3. 在单元格中设置约束或布局集合视图。
  4. 将集合视图的数据源设置为托管的 Table 视图控制器。
  5. 向 Table 视图控制器添加 UICollectionViewDataSource 一致性。
  6. 实现UICollectionViewDataSource的方法,即UITableViewController中的collectionView:numberOfItemsInSection:collectionView:cellForItemAtIndexPath:

如果您知道如何使用 UITableViewUICollectionView,这应该不难理解。

更新 您的代码看起来应该可以正常工作。 所以,你应该检查是否:

  1. 您确实将 Table 视图控制器的 class 设置为您的 FeaturedController class。
  2. 您确实已将 Interface Builder 中的集合视图连接到 popularCollectionView
  3. 您已经有了标识符为 popular 的集合视图单元原型。虽然,如果你没有这样做,它应该会崩溃。
  4. 在 IB 中,您已经将 Table 视图设置为静态。

我在这里做了一个小例子

橙色视图是 Collection View,绿色视图是原型 Collection View Cell,标识符为 myCell

然后我将我的视图控制器设置为集合视图的数据源。但是你也可以像你一样在代码中设置它。

然后我实现下面的数据源方法。

@interface ViewController () <UICollectionViewDataSource>

@end

@implementation ViewController

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath];
    return cell;
}

@end

这是结果:

对于 TableView 静态单元格:

在连接了集合视图的视图控制器中,添加视图加载委托和集合视图的数据源,这会将委托添加到表视图 cell,如果你用故事板做的,你把它连接错了,因为委托和数据源必须在单元格初始化中。

@IBOutlet weak var quotesCollectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()

    quotesCollectionView.delegate = self
    quotesCollectionView.dataSource = self
}                                                                                           

对于 TableView 动态单元格:

class QuotesTableViewCell: UITableViewCell {

// MARK: Properties

@IBOutlet weak var quotesCollectionView: UICollectionView!

// MARK: Initialization

override func awakeFromNib() {
    super.awakeFromNib()
    quotesCollectionView.delegate = self
    quotesCollectionView.dataSource = self
   }
}