如何在表视图的每一行中添加添加标题

Iam使用表格视图内部的集合视图构建iOS应用程序.Iam每行具有三行,其内部具有集合视图.Iam计划每行每个部分具有三个部分。例如,第一行应该在单独的部分中,标题与第二行和第三行的相似之处。 每当我创建三个部分时,我都会获取所有三个部分中的所有三行。我希望每个行都有一个单独的带有标题的部分。

import UIKit

class StoreVC: UIViewController,UITableViewDelegate,UITableViewDataSource {


    @IBOutlet weak var CoursetableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        CoursetableView.tableFooterView = UIView()

        CoursetableView.delegate = self
        CoursetableView.dataSource = self
    }

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

    func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
        if section == 0
        {
            return "Courses"
        }

        else if section == 1
        {
            return "Tests"
        }

        return "Bundles"
    }



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



    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        if indexPath.row == 0
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! CourseRow

            return cell
        }

        else if indexPath.row == 1
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "testcell",for: indexPath) as! TestRow

            return cell
        }

        else if indexPath.row == 2

        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "bundlecell",for: indexPath) as! BundleRow

            return cell
        }

        return UITableViewCell()



    }





}
pk110987 回答:如何在表视图的每一行中添加添加标题

在您的Xcode游乐场上尝试此代码,并根据需要进行自定义。 导入UIKit 导入PlaygroundSupport

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

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

    override func tableView(_ tableView: UITableView,viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UILabel()
        headerView.text = "Header: \(section)"
        return headerView
    }

    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = "Cell: \(indexPath)"
        return cell
    }
}

PlaygroundPage.current.liveView = ViewController()
本文链接:https://www.f2er.com/2327716.html

大家都在问