【问题标题】:Is it possible to have a UITableView under a UICollectionView?是否可以在 UICollectionView 下有一个 UITableView?
【发布时间】:2019-11-13 02:28:52
【问题描述】:

长话短说。我有一个 ViewController,它有一个 ColletionView,它是页面的主要组件。 CollectionView 有 2 个可重复使用的单元格,一个页眉和一个页脚。我想在页脚中有一个 TableView,但这是不可能的,当我尝试连接 TableView 时出现以下错误:

“非法配置:ViewController到UITableView的tableView outlet无效。Outlets无法连接重复内容。”

我的建议是,我想以完全错误的方式实现这一目标。

我想实现以下目标:

【问题讨论】:

  • @richard 我建议您使用单独的 tableView 和 collectionView 以获得更好的体验,即使您可以按照自己的方式实现这一点,但这可能会有些棘手和困难。
  • @ShivJaiswal 我想使用这种方法,因为我想要一个动态增长的 CollectionView(项目编号)+ 我想要一个滚动页面。
  • @richardtulkan 哦..那么我建议你在tableView中使用collectionView,反之亦然。
  • @richardtulkan - 您应该能够使用“普通”UIView,将表格视图作为子视图,作为集合视图页脚视图 可重用 视图。那么它就不会重复内容了,你应该可以正确连接你的table view了。

标签: ios swift uitableview uicollectionview


【解决方案1】:

我假设您确实像这样注册了页脚/页眉笔尖:

segmentCollectionView.register(UINib(nibName: /* CellNibName */, bundle: nil), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: /* CellId */)

然后像这样将单元格出列

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    if kind == UICollectionView.elementKindSectionFooter {
        let cell = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: /* CellId */, for: indexPath) as! /* Your Class */
    } 
    else {
    }
}
  • 所以诀窍是在您注册的单元格类中作为页脚,您可以转到 xib 文件并在引用中添加 UITableView /* Your Class */
  • 链接UITableView in the cell class not theUIViewControllerclass that contains theUICollectionView`
  • 然后在出列单元格并在单元格类中执行所有业务时传递您想要的数据

【讨论】:

  • 不敢相信,但我解决了。不完全像你说的那样,而是因为你,非常感谢你的时间和精力,我欠你的。 @karem_gohar
  • @richardtulkan 不客气,只要确保当你获得知识时将其传递给其他人 :-)
【解决方案2】:

[已解决]

解决方案如下:我为页脚制作了​​一个 CollectionReusableView 文件,然后恰好在@karem_gohar 说我通过了以下内容时:

footer.tableView.delegate = footer.self footer.tableView.dataSource = footer.self

我将 tableView 连接到 CollectionReusableView,我创建了一个数组来测试它我用数组的元素填充了 tableView。

Ps.:我制作了一个 TableViewCell 并将一个标签链接到它。标签显示了数组的元素。

【讨论】:

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