1

我是一個新的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的背景顏色設置爲橙色時開始)。任何幫助將不勝感激。

預先感謝您。

+0

你需要添加一個tableview到你的單元格。你是在故事板還是NIB文件中定義單元格?或者你想以編程方式做所有事情? – Paulw11

回答

0

要做到這一點完全編程方式添加不使用筆尖/ XIB文件,你需要做一個你可以這樣做:

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

謝謝,我昨天晚上在想這件事,你已經證實了我的答案!欣賞它一堆! –

+0

@MikeH如果這是正確的答案,請接受它,謝謝 – 3stud1ant3

1

您需要在您的NotToDoListCell從廈門國際銀行文件中添加tableView如出口與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 
} 
}