我是一個新的Swift程序員,我目前正在研究一個簡單的待辦事項列表應用程序。問題在於我想要顯示一個非工作清單。此外,我正在嘗試通過代碼完成此操作。如何在UICollectionViewCell中顯示UITableView
我希望用戶能夠2個CollectionViewCells之間水平滾動,每有一個TableView中在其中顯示「待辦列表」和「不要做清單」。
到目前爲止,我有一個創建具有每個填補了窗口2個定製單元一UICollectionView一個視圖控制器,因此用戶可以在它們之間滾動。
我工作的「不求列表」單元格,我想在它顯示的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影片」,但我看到的只是一個橙色屏幕(從將NotToDoListCell的背景顏色設置爲橙色時開始)。任何幫助將不勝感激。
預先感謝您。
你需要添加一個tableview到你的單元格。你是在故事板還是NIB文件中定義單元格?或者你想以編程方式做所有事情? – Paulw11