Nolan Ranolin
Nolan Ranolin

Reputation: 409

How to put a tableView inside a section Header

I’m trying to put a tableView inside a collectionView sectionHeader. I tried adding a tableView to the header in storyboard and then setting its class to tableViewController.swift but that didn’t work. (I’m using swift)

Upvotes: 1

Views: 778

Answers (1)

Nolan Ranolin
Nolan Ranolin

Reputation: 409

After trying a bit i did manage to get it working properly. First i created a header as normal, then created an IBOutlet of the tableView. Then I simply setup the header file like a tableViewController is setup, connected to the dataSource and delegate in awakeFromNib and done.

class HomeHeader: UICollectionReusableView, UITableViewDataSource, UITableViewDelegate {


    @IBOutlet weak var tableView: UITableView!


    override func awakeFromNib() {

        tableView.delegate = self
        tableView.dataSource = self
       //tableView.backgroundColor = UIColor.clear
    }

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1 //number of sections
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


        return 5 //number of cells
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
        cell.textLabel?.text = "test"
       // cell.backgroundColor = UIColor.clear

        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    }



}

Upvotes: 1

Related Questions