我是一名新的 Swift 程序员,我目前正在开发一个非常简单的待办事项列表应用程序。问题是我也想显示一个待办事项列表。此外,我正在尝试通过代码来完成这一切。
我希望用户能够在 2 个 CollectionViewCell 之间水平滚动,每个 CollectionViewCell 中都有一个 TableView,显示“待办事项列表”和“未办事项列表”。
到目前为止,我有一个 ViewController,它创建了一个 UICollectionView,它有 2 个自定义单元格,每个单元格都填满了窗口,因此用户可以在它们之间滚动。
我正在处理“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
}
}
我希望表格 View 会显示 3 行文本,其中包含:“玩视频游戏”、“外出就餐”和“观看 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
}
}
关于ios - 如何在 UICollectionViewCell 中显示 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45767502/