简体   繁体   中英

UITableView Showing only one section

I have the following UIViewController which contains a UITableView :

class Home : UIViewController {

    override func loadView() {
        self.view = HomeView()
    }

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

with this view:

class HomeView: UIView {

let tableView = UITableView()
let delegate = TVDelegate()
let dataSource = TVDataSource()

override init(frame: CGRect) {
    super.init(frame: frame)
    createSubviews()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    createSubviews()
}

func createSubviews() {
   setupTableView()
   setupConstraints()
}

func setupTableView() {
    tableView.estimatedRowHeight = 500
    tableView.tableFooterView = UIView()
    tableView.delegate = delegate
    tableView.dataSource = dataSource
    tableView.backgroundColor = UIColor.purple
    tableView.register(TVCell.self, forCellReuseIdentifier: "tableViewCell")
}

func setupConstraints() {
    tableView.prepareView()
    self.addFullSizeView(item: tableView)
}
}

This is my delegate:

class TVDelegate : NSObject, UITableViewDelegate {

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

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 500
}

}

and my data source:

class TVDataSource : NSObject, UITableViewDataSource {

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as? TVCell
    {
        return cell
    }
    return UITableViewCell()
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "Your Favourites"
}

}

However, instead of seeing 5 sections, I only see one section with one heading when I run my app.

numberOfSections is a dataSource method should be here

class TVDataSource : NSObject, UITableViewDataSource { 
  func numberOfSections(in tableView: UITableView) -> Int {
     return 5
  }
}

What happens is that the tableView takes the default number which is 1 where it's not implemented in TVDataSource class as the method is optional

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM