如何使用 Swift 在 UICollection 视图单元格中添加 Table 视图?
How do i add a Table View inside a UICollection View Cell Using Swift?
如何在 UICollectionViewCell 中添加 table 视图?我不确定真的要开始了。我如何获取 table 视图数据源并委托出去?这是我已经尝试过的...
我试过 Google 搜索它,但我得到的只是如何在 table 视图中添加集合视图,而不是相反。
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
}
在UICollectionView
(单元格)中添加UITableView
反之亦然。
你只需要清楚谁是UITableView's
委托和数据源。根据您的代码,它是GeneralAisleViewController
。
然后您需要做的是将UITableView
添加到单元格的子视图中,例如:
代码:
...
let TableView = cell.viewWithTag(2) as! UITableView
self.tableView = TableView
cell.addSubview(TableView)
return cell
然后 table 视图将从其数据源中获取 table 视图单元格。
然而,事情从来没有看起来那么简单。
UITableView
和 UICollectionView
都共享它们的单元格的 reuse
技术,所以每次你从队列中得到一个单元格时,它们都会被重用,而不是创建,所以你需要非常小心得到细胞后处理它们。
最好在将其他属性或视图分配给单元格之前使用 prepareReuse
清理单元格的子视图。
如何在 UICollectionViewCell 中添加 table 视图?我不确定真的要开始了。我如何获取 table 视图数据源并委托出去?这是我已经尝试过的...
我试过 Google 搜索它,但我得到的只是如何在 table 视图中添加集合视图,而不是相反。
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
}
在UICollectionView
(单元格)中添加UITableView
反之亦然。
你只需要清楚谁是UITableView's
委托和数据源。根据您的代码,它是GeneralAisleViewController
。
然后您需要做的是将UITableView
添加到单元格的子视图中,例如:
代码:
...
let TableView = cell.viewWithTag(2) as! UITableView
self.tableView = TableView
cell.addSubview(TableView)
return cell
然后 table 视图将从其数据源中获取 table 视图单元格。
然而,事情从来没有看起来那么简单。
UITableView
和 UICollectionView
都共享它们的单元格的 reuse
技术,所以每次你从队列中得到一个单元格时,它们都会被重用,而不是创建,所以你需要非常小心得到细胞后处理它们。
最好在将其他属性或视图分配给单元格之前使用 prepareReuse
清理单元格的子视图。