numberOfSectionsInTableView无法正常工作

时间:2016-11-04 04:20:06

标签: ios swift uitableview 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)
    }  
}

我的numberOfSectionsInTableView从未被调用,我无法获得2个部分。

3 个答案:

答案 0 :(得分:16)

从您的代码中,我相信您使用的是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) {

}

正如您所看到的,您的numberOfSections函数的语法错误。这就是原因。

答案 1 :(得分:0)

这实际上是由于访问级别的解析。当我们自己不指定“ public”时,编译器会将其解析为一些私有实现的函数,甚至警告它几乎与public函数匹配。如果忽略这一点,那么它将忽略此函数,而是调用默认实现。希望这对某人有帮助。

答案 2 :(得分:-2)

func numberOfSectionsInTableView(tableView:UITableView) - > Int {         返回1     }