我是一名新的Swift程序员,目前正在开发一个非常简单的待办事项列表应用程序。问题是我想要显示一个不待办事项的清单。另外,我试图通过代码完成所有这些。
我希望用户能够在2个CollectionViewCell之间水平滚动,每个CollectionViewCells中都有一个TableView,显示一个待办事项列表'以及“不要做清单”#39;
到目前为止,我有一个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
}
}
我希望表格视图会显示3行文字,其中包含:"播放视频游戏","吃掉"和"观看Netflix&#34 ;但我所看到的只是一个橙色屏幕(从我将NotToDoListCell的背景颜色设置为橙色时)。任何帮助将不胜感激。
提前谢谢你。
答案 0 :(得分:1)
您需要在tableView
中添加NotToDoListCell
作为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
}
}
答案 1 :(得分:0)
要在不使用nib / xib文件的情况下以编程方式完成此操作,您需要在collectionview单元格中创建一个表视图,您可以执行以下操作:
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
}
}