如何在 UICollectionViewCell 中显示 UITableView [英] How do I show a UITableView within a UICollectionViewCell

查看:20
本文介绍了如何在 UICollectionViewCell 中显示 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的 Swift 程序员,目前正在开发一个非常简单的待办事项列表应用程序.问题是我也想显示一个不要做的清单.此外,我试图通过代码来做到这一点.

I am a new Swift programmer and I am currently working on a very simple to-do-list application. The catch is that I want to display a not-to-do list as well. Additionally, I am attempting to do this all through code.

我希望用户能够在 2 个 CollectionViewCells 之间水平滚动,每个 CollectionViewCells 中都有一个 TableView,显示待办事项列表"和非待办事项列表".

I want the user to be able to scroll horizontally between 2 CollectionViewCells, that each have a TableView within them displaying a 'To Do List' and a 'Not To Do List'.

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

So far I have a ViewController that creates a UICollectionView that has 2 custom cells that each fills up the window, so the user can scroll between them.

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

I am working on the "Not To Do List" cell, and I am trying to display a TableView within it, but I am having difficulty displaying it.

这是我的代码:

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
}
}

我希望表格视图会显示 3 行文本,其中包含:玩视频游戏"、外出就餐"和观看 Netflix",但我看到的只是一个橙色屏幕(从我设置NotToDoListCell 的背景颜色为橙色).任何帮助将不胜感激.

I would expect that the table view would show up with 3 rows of text containing: "Play Video Games", "Eat Out", and "Watch Netflix" but all I see is an orange screen (from when I set the background color of the NotToDoListCell to orange). Any help would be greatly appreciated.

先谢谢你.

推荐答案

要在不使用 nib/xib 文件的情况下完全以编程方式执行此操作,您需要在 collectionview 单元格中创建一个表视图,您可以执行以下操作:

To do this completely programmatically without using a nib/xib file , you need to make a table view inside a collectionview cell , you can do something like this:

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
}
}

这篇关于如何在 UICollectionViewCell 中显示 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆