如何将一个tableView放在一个Header部分中

时间:2017-07-01 08:41:52

标签: ios swift uitableview uicollectionview

我正在尝试将tableView放在collectionView sectionHeader中。我尝试将一个tableView添加到storyboard中的标题,然后将其类设置为tableViewController.swift,但这不起作用。 (我正在使用swift)

1 个答案:

答案 0 :(得分:1)

尝试了一下后,我确实设法让它正常工作。 首先我正常创建一个标题,然后创建一个tableView的IBOutlet。 然后我只需像设置tableViewController一样设置头文件,连接到数据源并在awakeFromNib中委托并完成。

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) {


    }



}
相关问题