numberOfSectionsInTableView不起作用 [英] numberOfSectionsInTableView not working

查看:405
本文介绍了numberOfSectionsInTableView不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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个部分.

My numberOfSectionsInTableView is never called and I can't get 2 sections.

推荐答案

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

From your code, I believe you are using Swift3. Then, the following are delegate and datasource methods for UITableView in Swift3

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

As you can see, your numberOfSections function's syntax is wrong.That is the reason.

这篇关于numberOfSectionsInTableView不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆