ios - 我正在尝试创建一个可扩展的 TableView ,其子元素具有 UICollectionView

标签 ios swift uitableview uicollectionview expandable-table

我正在尝试创建一个 TableView

  1. 可扩展
  2. 它的 Childview 有一个 UICollectionView
  3. UICollectionView 使用 API 动态呈现
  4. 应注意 UICollectionView Item 的 onTouch 事件

尝试了几个示例均无效

import UIKit

extension String{
    func print(){
        Swift.print(self)
    }

    func log(){
        Swift.print(self)
    }
}


class CustomVCViewController: UIViewController, UITableViewDataSource, UITableViewDelegate , UICollectionViewDataSource, UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ccell", for: indexPath) as! CustomCollectionViewCell
        cell.backgroundColor = UIColor.blue
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return 10
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        "Selection of Collection View item at \(indexPath.row)".print()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        "Selection of Table View item at \(indexPath.row)".print()
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! CustomTableViewCell
        cell.innerCollectionView.dataSource = self
        cell.innerCollectionView.delegate = self
        cell.backgroundColor = UIColor.red

        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }


    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()


        tableView.delegate = self

        tableView.dataSource = self
        // Do any additional setup after loading the view.
    }
}
  1. This is for the table view cell
 import UIKit        
 class CustomTableViewCell: UITableViewCell {

        @IBOutlet weak var innerCollectionView: UICollectionView!
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }

        override func setSelected(_ selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)

            // Configure the view for the selected state
        }
 }

最佳答案

根据我的理解,您希望 tableviewcell 可扩展为 Collection View ,因此我为其添加代码,问题是 collectionview 并未相应地使其第一个内部内容大小无效

    /// This CollectionView class is used to invalidate collectionview's default implementation of inrinsic content size

    class DynamicCollectionView: UICollectionView {
        override func layoutSubviews() {
            super.layoutSubviews()
            if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
                self.invalidateIntrinsicContentSize()
            }
        }

        override var intrinsicContentSize: CGSize {
            return collectionViewLayout.collectionViewContentSize
        } 
}

所以将此类添加到您的collectionview中,并为您的collectionviewcells提供flowlayout。还!!!在 tableview 的 heightforRowAt 上返回 UITableViewAutomaticDimension。

关于ios - 我正在尝试创建一个可扩展的 TableView ,其子元素具有 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56110557/

相关文章:

iOS 4.3 Statusbar隐藏显示白条

ios - WKWebView 委托(delegate)问题(使用 Swift 的 Xcode 7.3)

ios - 检查互联网连接类型 (iOS)

swift - 如何设置tableView单元格的边距?

ios - 如何在 Storyboard中创建一个按钮以启动在 xcode swift 中使用 spritekit 创建的游戏

ios7 - 在 Parse 后端删除一个 tableview 对象

ios - 从互联网获取表数据时,有人可以用UItableview来解释MVC吗?

ios - 表格 View 单元格的附件 View 仅显示在最后一个单元格中

objective-c - 在 iPad native 应用程序中每月订阅的 Paypal 付款

iOS 编程蜂窝数据控制