ios - numberOfSectionsInTableView 不工作

标签 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 个部分。

最佳答案

从您的代码来看,我相信您使用的是 Swift3。那么,下面是Swift3中UITableView的delegate和datasource方法

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 函数的语法是错误的。这就是原因。

关于ios - numberOfSectionsInTableView 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40415223/

相关文章:

ios - 冗余约束 'Self' : 'AnyObject'

ios - STTwitter swift 类型 'Any' 在 getHomeTimeline 方法上没有下标成员

json - 在浏览器和 Xcode 控制台中获取不同的 JSON 响应

iphone - 如何同时从 UITableView 插入和删除一行

为 webView 加载设置 iOS 超时?

ios - 静态库代码阶段项目头

ios - 在 View Controller 中绑定(bind)元组或字符串以使用 RXSwift 查看模型

swift - 如何在 Collection View 中设置图像半径?

swift - 在 Swift 中设置 UITextView 文本

ios - 使用 UITableViewAutomaticDimension 时 UITableView 在 begin/endUpdates 之后跳转