表视图数据未显示SWIFT



试图在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;
    }
}

更改您的代码

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
    }

首先,您在 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")
  }
}

最新更新