1

Ich bin ein neuer Swift-Programmierer und ich arbeite derzeit an einer sehr einfache to-do-Liste Anwendung. Der Catch ist, dass ich auch eine nicht zu erledigende Liste anzeigen möchte. Außerdem versuche ich, dies durchgängig zu tun.Wie zeige ich eine UITableView innerhalb einer UICollectionViewCell

Ich möchte der Benutzer in der Lage sein, horizontal zwischen 2 CollectionViewCells, die jeweils eine TableView mit einer "To-Do-Liste" und eine "Not To-Do-Liste" haben scrollen.

Bis jetzt habe ich eine ViewController, die eine UICollectionView erstellt, die 2 benutzerdefinierte Zellen hat, die jeweils das Fenster füllt, so dass der Benutzer zwischen ihnen scrollen kann.

Ich arbeite an der "Not To List" -Zelle, und ich versuche, ein TableView darin anzuzeigen, aber ich habe Schwierigkeiten, es anzuzeigen.

Hier ist mein Code:

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

ich, dass die Tabellenansicht mit drei Textzeilen enthalten, würde zeigen, erwarten würde: "Play Video Games", "Eat Out" und "Watch Netflix" aber Alles, was ich sehe, ist ein orangefarbener Bildschirm (ab dem ich die Hintergrundfarbe der NotToDoListCell auf Orange eingestellt habe). Jede Hilfe würde sehr geschätzt werden.

Vielen Dank im Voraus.

+0

Sie müssen eine Tabellenansicht zu Ihrer Zelle hinzufügen. Definieren Sie die Zelle in einem Storyboard oder einer NIB-Datei? Oder willst du alles programmatisch machen? – Paulw11

Antwort

0

Um dies zu tun vollständig programmatisch hinzugefügt werden, ohne Verwendung einer Feder/xib Datei, müssen Sie eine machen Tabellenansicht in einer collectionview-Zelle, können Sie beispielsweise Folgendes tun:

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

Danke, ich habe letzte Nacht darüber nachgedacht und du hast meine Antwort bestätigt! Schätze es einen Haufen! –

+0

@MikeH wenn dies die richtige Antwort ist, bitte akzeptieren Sie es, danke – 3stud1ant3

1

Sie müssen die tableView in Ihrem NotToDoListCell als Austritt aus Ihrer xib Datei hinzufügen und in der awakeFromNib Methode 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 
} 
} 
Verwandte Themen