如何在 UICollectionViewCell 中显示 UITableView

How do I show a UITableView within a UICollectionViewCell

我是一名新的 Swift 程序员,我目前正在开发一个 非常 简单的待办事项列表应用程序。问题是我也想显示一个待办事项列表。此外,我正在尝试通过代码来完成这一切。

我希望用户能够在 2 个 CollectionViewCells 之间水平滚动,每个 CollectionViewCells 中都有一个 TableView,显示 'To Do List' 和 'Not To Do List'。

到目前为止,我有一个 ViewController 创建了一个 UICollectionView,它有 2 个自定义单元格,每个单元格都填充了 window,因此用户可以在它们之间滚动。

我正在处理 "Not To Do List" 单元格,我试图在其中显示一个 TableView,但我无法显示它。

这是我的代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
...
collectionView.register(ToDoListCell.self, forCellWithReuseIdentifier: toDoListCellid)
collectionView.register(NotToDoListCell.self, forCellWithReuseIdentifier: notToDoListCellid)
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if (indexPath.item == 0) {
        let toDoCell = collectionView.dequeueReusableCell(withReuseIdentifier: toDoListCellid, for: indexPath)
        return toDoCell
    }
    else {
        let notToDoCell = collectionView.dequeueReusableCell(withReuseIdentifier: notToDoListCellid, for: indexPath)
        return notToDoCell
    }
}



class NotToDoListCell: UICollectionViewCell {

var ntdlArray = ["Play Video Games", "Eat out", "Watch Netflix"]
let ntdlCell = "ntdlCell"

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.orange
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}



extension NotToDoListCell: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return ntdlArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: ntdlCell, for: indexPath)

    cell.textLabel?.text = ntdlArray[indexPath.item]

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}
}

我希望 table 视图会显示 3 行文本,其中包含:"Play Video Games"、"Eat Out" 和 "Watch Netflix",但我只看到一个橙色屏幕(当我将 NotToDoListCell 的背景颜色设置为橙色时)。任何帮助将不胜感激。

提前谢谢你。

您需要在 NotToDoListCell 中添加 tableView 作为 xib 文件的出口,并添加 awakeFromNib 方法 self.tableView.dataSource = self

class NotToDoListCell: UICollectionViewCell {

var ntdlArray = ["Play Video Games", "Eat out", "Watch Netflix"]
let ntdlCell = "ntdlCell"

override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.orange
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

override func awakeFromNib(){
    super.awakeFromNib()
    self.tableView.dataSource = self
}


extension NotToDoListCell: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return ntdlArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: ntdlCell, for: indexPath)

    cell.textLabel?.text = ntdlArray[indexPath.item]

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}
}

要完全以编程方式完成此操作而不使用 nib/xib 文件,您需要在 collectionview 单元格内创建一个 table 视图,您可以这样做:

class NotToDoListCell: UICollectionViewCell {

   lazy var tableView: UITableView = {
         let tblView = UITableView()
         tblView.delegate = self
         tblView.dataSource = self
         tblView.translatesAutoresizingMaskIntoConstraints = false
         return tblView
   }()

var ntdlArray = ["Play Video Games", "Eat out", "Watch Netflix"]
let ntdlCell = "ntdlCell"


override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.orange

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: ntdlCell) 
    setupTableView()

}
func setupTableView() {

    addSubview(tableView)
    tableView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    tableView.bottom.constraint(equalTo: bottomAnchor).isActive = true
    tableView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    tableView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true 


}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}



extension NotToDoListCell: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return ntdlArray.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: ntdlCell, for: indexPath)

    cell.textLabel?.text = ntdlArray[indexPath.item]

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 50
}
}