TableView内部特定的UICollectionViewCell以编程方式?

我想在UICollectionViewCell中添加一个TableView,所以我想自己从TableView管理那个单元格。 所以更清楚的是,如果indexPath.row是2,那么我想调用我的tableView单元格内部的collectionview indexPath.row。

请检查图片,我用红色做了我想做的事。 我使用UICollectionViewController和UICollectionViewControllerFlowLayout以编程方式创建了所有内容。

对于那些需要它的人,我找到了解决方案:

class CustomizedCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate { var tableView = UITableView() let cellIdentifier: String = "tableCell" override func layoutSubviews() { super.layoutSubviews() tableView.delegate = self tableView.dataSource = self tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier) } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 4 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: cellIdentifier) cell.textLabel?.text = "1 CUP" cell.detailTextLabel?.text = "Whole" return cell } 

}

然后在CollectionView的viewDidLoad方法我这样做:

 collectionView?.register(CustomizedCell.self, forCellWithReuseIdentifier: "cell") 

之后,我在UICollectionView的cellForRowAt indexPath方法中调用了这样的:

 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomizedCell return cell } 

您可以为该索引路径创建自定义UICollectionView单元格。 并在单元格xib中添加tableview。

在自定义单元类中实现tableview的委托方法。

  class CustomCollectionViewCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate { @IBOutlet var tableView: UITableView! override func layoutSubviews() { super.layoutSubviews() tableView.delegate = self tableView.dataSource = self } } 

我找到了一个适合我的解决方案。

 import UIKit class FirstCollectionViewCell: UICollectionViewCell { let cellId = "cellId" let tableView:UITableView = { let tableView = UITableView() tableView.translatesAutoresizingMaskIntoConstraints = false return tableView }() override init(frame: CGRect) { super.init(frame: frame) backgroundColor = .blue addSubview(tableView) setUpViews() addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":tableView])) addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":tableView])) } required init?(coder aDecoder: NSCoder) { fatalError("init is not done") } func setUpViews() { tableView.delegate = self tableView.dataSource = self tableView.register(MyCell.self, forCellReuseIdentifier: cellId) } } extension FirstCollectionViewCell: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! MyCell cell.label.text = "Testing testing" return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 40.0 } } class MyCell:UITableViewCell { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setUpViews() } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } // creating a label to display some dummy text let label:UILabel = { let label = UILabel() label.text = "test" label.textAlignment = .center label.translatesAutoresizingMaskIntoConstraints = false return label }() func setUpViews() { addSubview(label) addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":label])) addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":label])) } }