numberOfSectionsInTableView无法正常工作

Wil*_*ang 3 uitableview ios swift swift3

import UIKit

class exploreViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var exploreTableView: UITableView!
    var CELLHEIGHT = 200
    var SECTIONHEADER = "SECTIONHEADER"

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        exploreTableView.delegate = self
        exploreTableView.dataSource = self
        exploreTableView.register(UINib(nibName: "answerCell", bundle: nil), forCellReuseIdentifier: "cell1")
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }

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

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.exploreTableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! answerCell
        cell.name.text = "222222"
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CGFloat(CELLHEIGHT)
    }  
}
Run Code Online (Sandbox Code Playgroud)

numberOfSectionsInTableView永远不会被打电话,我不能得到2个部分.

Kri*_*aCA 18

从您的代码中,我相信您正在使用Swift3.然后,以下是Swift3中UITableView的委托和数据源方法

func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)

    // Configure the cell...

    return cell
}

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

}
Run Code Online (Sandbox Code Playgroud)

如您所见,您的numberOfSections函数的语法是错误的.这就是原因.