【问题标题】:How do i add a Table View inside a UICollection View Cell Using Swift?如何使用 Swift 在集合视图单元格中添加表视图?
【发布时间】:2015-06-03 22:50:01
【问题描述】:

如何在 UICollectionViewCell 中添加表格视图?我不确定真的要开始。如何获取表视图数据源并委派出去?这是我已经尝试过的......

我试过谷歌搜索它,但我得到的只是如何在表格视图中添加集合视图,而不是相反。

 import UIKit

    class GeneralAisleViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate {

        @IBOutlet weak var collectionView: UICollectionView!


        var tableView:UITableView = UITableView()

        var CollectionViewArray = ["1", "2", "3"]

var tableView1Array = ["1", "2"]
        var tableView2Array = ["1", "2"]
var tableView3Array = ["1", "2"]


        override func viewDidLoad() {
            super.viewDidLoad()

            // Do any additional setup after loading the view.


            self.collectionView.delegate = self
            self.collectionView.dataSource = self

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

        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


        func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return CollectionViewArray.count
        }


        func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

            let cell:UICollectionViewCell = self.collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UICollectionViewCell


            let dataToDisplay:String = CollectionViewArray[indexPath.row]

            let dataLabel:UILabel = cell.viewWithTag(1) as! UILabel




            dataLabel.text = dataToDisplay


            let TableView = cell.viewWithTag(2) as! UITableView

            self.tableView = TableView




            return cell
        }

【问题讨论】:

    标签: ios uitableview swift ios8 uicollectionview


    【解决方案1】:

    在aUICollectionView(Cell) 中添加aUITableView 反之亦然。

    您只需要清楚谁是UITableView's 委托和数据源。根据您的代码,它是GeneralAisleViewController

    那么你需要做的就是将UITableView添加到单元格的子视图中,比如:

    代码:

    ...
    let TableView = cell.viewWithTag(2) as! UITableView
    self.tableView = TableView
    cell.addSubview(TableView)
    return cell
    

    然后表格视图将从其数据源中获取表格视图单元格。

    然而,事情从来都没有看起来那么简单。 UITableViewUICollectionView 都为他们的单元共享reuse 技术,所以每次你从队列中得到一个单元时,它们都会被重用,而不是创建,所以你需要非常小心地处理它们。细胞。

    在将其他属性或视图分配到单元格之前,最好使用prepareReuse 清理单元格的子视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 2018-07-23
      • 2018-07-19
      • 2020-04-16
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      相关资源
      最近更新 更多