【问题标题】:UICollectionView inside Static TableViewCell静态 TableViewCell 内的 UICollectionView
【发布时间】:2015-05-30 18:23:59
【问题描述】:

我在网上看到了许多关于将 UICollectionViews 嵌入动态表格视图单元格和子类化集合视图以设置委托的在线教程,但我想知道该过程对于静态表格视图单元格是否有任何不同。

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

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

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
    }

非常感谢!

【问题讨论】:

    标签: ios uitableview swift uicollectionview


    【解决方案1】:

    如果您知道如何将集合视图添加到动态表格视图单元格,那么将其添加到静态单元格会容易得多。您根本不需要对任何东西进行子类化(但这样做可能是个好主意)。在底层,静态表格视图只不过是一个普通的表格视图,它得到了主机 UITableViewController 的支持,可以自动设置您在 Interface Builder 中的布局。所以,这里是如何做到这一点:

    1. 从 Interface Builder 中的 Object Library 中拖动一个 Collection View 并将其放置在所需的单元格中。
    2. 确保 Table View 由 Table View Controller 托管。
    3. 在单元格中设置约束或布局集合视图。
    4. 将 Collection View 的 dataSource 设置为托管的 Table View Controller。
    5. UICollectionViewDataSource 一致性添加到表视图控制器。
    6. UITableViewController中实现UICollectionViewDataSource的方法,即collectionView:numberOfItemsInSection:collectionView:cellForItemAtIndexPath:

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

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

    1. 您确实已将 Table View Controller 的类设置为您的 FeaturedController 类。
    2. 您确实已将 Interface Builder 中的 Collection View 连接到 popularCollectionView
    3. 您已经拥有一个标识符为popular 的原型集合视图单元。不过,如果您没有这样做,它应该会崩溃。
    4. 在 IB 中,您已经将表视图设置为静态。

    我在这里做了一个小例子

    橙色视图是集合视图,绿色视图是原型集合视图单元格,标识符为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
    

    结果如下:

    【讨论】:

    • 仍然没有显示,但没有错误。会不会是我的限制?
    • 你设置好数据源了吗?您可以查看视图调试器以查看集合视图是否真的存在。
    • 集合视图在那里,但没有一个单元格(我在视图调试模式下检查。我正在更新帖子以显示我拥有的内容
    • 我已经更新了我的答案。基本上,尝试检查您是否正确设置了插座和视图控制器类。
    • 我检查并重新检查了所有内容,但没有任何显示。我只是要删除所有内容并重新启动。不过感谢您的宝贵时间
    【解决方案2】:

    对于 TableView 静态单元格

    在您连接集合视图的视图控制器中,添加视图并为集合视图加载代理和数据源,这会将代理添加到 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
       }
    }                                                                                                
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多