numberOfSectionsInTableView not working

后端 未结 3 1399
清歌不尽
清歌不尽 2021-01-06 09:53
import UIKit

class exploreViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutle         


        
相关标签:
3条回答
  • 2021-01-06 10:35

    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) {
    
    }
    

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

    0 讨论(0)
  • 2021-01-06 10:52

    This is actually due to access level resolution. When we don't specify the "public" ourselves, the compiler resolves it as to be some privately implemented function and even warns that it nearly matches a public function. If one ignores this then it neglects this function and rather calls the default implementation. Hope this helps someone.

    0 讨论(0)
  • 2021-01-06 10:56

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

    0 讨论(0)
提交回复
热议问题