0

I want to display UICollectionView inside static TableViewCell

I have a static TableViewCell that contains CollectionView. Well, the problem is that, in my opinion, code looks good but when I launch program, TableViewCell doesn't show me CollectionView. I'm a little bit confused, so I will be so happy if you help me. Thanks

TableViewController Class:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var Cell = tableView.dequeueReusableCell(withIdentifier: "Action")
    switch indexPath.section {
        case 0:
            tableView.register(ScheduleCell.self, forCellReuseIdentifier: "Schedule")
            Cell = tableView.dequeueReusableCell(withIdentifier: "Schedule") as! ScheduleCell

        case 1:
            return Cell!

        case 2:
            tableView.register(MarksCell.self, forCellReuseIdentifier: "Marks")
            Cell = tableView.dequeueReusableCell(withIdentifier: "Marks") as! MarksCell

        default:
            Cell = tableView.dequeueReusableCell(withIdentifier: "Action") as! ActionCell
    }

    return Cell!
}

ActionCell:

extension ActionCell: UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ActionCell", for: indexPath) as! ActionsCollectionViewCell
        var Labels = ["РАСПИСАНИЕ", "ДОМАШНИЕ ЗАДАНИЯ", "ОЦЕНКИ"]
        var Icons = ["Safari", "DZ", "Rating"]
        var Colors = [UIColor.init(red: 26/255, green: 196/255, blue: 234/255, alpha: 1),
                  UIColor.init(red: 251/255, green: 143/255, blue: 25/255, alpha: 1),
                  UIColor.init(red: 251/255, green: 25/255, blue: 118/255, alpha: 1)]
        cell.Title.text = Labels[indexPath.item]
        cell.Icon.image = UIImage.init(named: Icons[indexPath.item])
        cell.Button.backgroundColor = Colors[indexPath.item]
        return cell
    }
}

Also, I have to notice that in Storyboard i mentioned all classes and ReuseIDs

2
  • Did you set delegate and datasource of collectionview ? Commented Dec 29, 2018 at 11:28
  • Yes, I did it... Commented Dec 29, 2018 at 11:44

1 Answer 1

1

If you're using 'UITableView' with Static Cells as a content you don't need to implement 'tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)' everything can be defined in Storyboard.

Here's how the UITableViewController would look like in storyboard, using custom cells (TableCell, CollectionCell) both for table and collection view, and having 'UITableViewCell' act as a data source for containing 'UICollectionView':

enter image description here

And here's UITableViewController implementation:

class ViewController: UITableViewController {

}

class TableCell: UITableViewCell {

}

class CollectionCell: UICollectionViewCell {

}

extension TableCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath)

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried to use your solution but I got this error: '-[UITableViewCell collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x106057200'. Sorry for the mistakes, i'm feeling so ashamed. My code is here: imgur.com/a/ONYziql
Have you set the dataSource to be your custom 'ActionCell'? Here's my sample project for reference
I found mistake! Thanks a lot. I'm a real idiot. Wishing you a Happy New Year

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.