tableview数据没有迅速显示出来

时间:2018-02-10 20:36:17

标签: ios swift uitableview

尝试在tableView中显示状态列表,但是当我运行它时,tableView中没有任何显示。其他一切似乎都在起作用。

import UIKit

class licenseViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
    @IBOutlet weak var tableView: UITableView!
    var items: [String] = ["Alabama", "Alaska", "Arizona" , "Arkansas","California", "Colorado" , "Connecticut","Delaware","Florida","Georgia" ,"Hawaii","Idaho","Illinois","Indiana","Iowa", "Kansas", "Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesotea","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvani","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }

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

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! UITableViewCell

        cell.textLabel?.text = self.items[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }
}

2 个答案:

答案 0 :(得分:0)

首先在numberOfRowsInSection中返回0,因此不会显示任何数据,您必须返回要显示的数组的计数,而您还有另一个数组。

其次你有2个cellForRow方法,一个是不推荐使用的,我离开了另一个在下面工作的方法,所以删除所有类中的方法并用这个替换所有方法:

import UIKit

class licenseViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{

   @IBOutlet weak var tableView: UITableView!

   var items: [String] = ["Alabama", "Alaska", "Arizona" , "Arkansas","California", "Colorado" , "Connecticut","Delaware","Florida","Georgia" ,"Hawaii","Idaho","Illinois","Indiana","Iowa", "Kansas", "Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesotea","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvani","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]

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

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     var cell:UITableViewCell =   self.tableView.dequeueReusableCell(withIdentifier: "cell") 

     cell.textLabel?.text = self.items[indexPath.row]

     return cell
  }

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

  override func viewDidLoad()
  {
    super.viewDidLoad()
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
  }

}

答案 1 :(得分:0)

像这样更改你的代码

import UIKit

class licenseViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
var items: [String] = ["Alabama", "Alaska", "Arizona" , "Arkansas","California", "Colorado" , "Connecticut","Delaware","Florida","Georgia" ,"Hawaii","Idaho","Illinois","Indiana","Iowa", "Kansas", "Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesotea","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvani","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"]

    override func viewDidLoad(){
        super.viewDidLoad()

        self.tableView.delegate = self
        self.tableView.dataSource = self

        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count // return the total items in the items array
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  self.tableView.dequeueReusableCell(withIdentifier: "cell")
        cell.textLabel?.text = self.items[indexPath.row]
        return cell
   }

添加此内容

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