我正在尝试将值从JSON加载到tableview。这是我的密码,我不知道我遗漏了什么?它显示了一个空白单元格列表,但没有任何值。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var tableView: UITableView!
    let baseURL = “<url>”
    var items = [UserObject]()

    override func viewDidLoad() {
        super.viewDidLoad()
        let frame:CGRect = CGRect(x: 0, y: 100, width: self.view.frame.width, height: self.view.frame.height-100)
        self.tableView = UITableView(frame: frame)
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.view.addSubview(self.tableView)

        getJSON()
        dispatch_async(dispatch_get_main_queue(),{
            self.tableView.reloadData()
        })
    }

    //5 rows
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("count /(self.items.count)")
        return self.items.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("CELL")
        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
        }
        let user = self.items[indexPath.row]
        print(self.items)
        cell!.textLabel?.text = user.name
        return cell!
    }


    func getJSON() {
        let url = NSURL(string: baseURL)
        print(url)
        let request = NSURLRequest(URL: url!)
        let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
        let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
            if error == nil {
                let swiftyJSON = JSON(data: data!)
                let results = swiftyJSON.arrayValue
                for entry in results {
                    self.items.append(UserObject(json: entry))
                }
            } else {
                print("error \(error)")
            }
        }

        task.resume()
    }
}

请注意,print("count /(self.items.count)")返回4,这是正确的,但是
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//function does not show any print statements.

}

最佳答案

异步请求完成后,您应该调用tableviewreloadData函数。

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var tableView: UITableView!
let baseURL = “<url>”
var items = [UserObject]()

override func viewDidLoad() {
  super.viewDidLoad()
  let frame:CGRect = CGRect(x: 0, y: 100, width: self.view.frame.width, height: self.view.frame.height-100)
  self.tableView = UITableView(frame: frame)
  self.tableView.dataSource = self
  self.tableView.delegate = self
  self.view.addSubview(self.tableView)
  getJSON()
}

//5 rows
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  print("count /(self.items.count)")
  return self.items.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  var cell = tableView.dequeueReusableCellWithIdentifier("CELL")
  if cell == nil {
    cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
  }
  let user = self.items[indexPath.row]
  print(self.items)
  cell!.textLabel?.text = user.name
  cell!.detailTextLabel?.text = user.date // UPDATE
  return cell!
}


func getJSON() {
  let url = NSURL(string: baseURL)
  print(url)
  let request = NSURLRequest(URL: url!)
  let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
  let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
    if error == nil {
      let swiftyJSON = JSON(data: data!)
      let results = swiftyJSON.arrayValue
      for entry in results {
        self.items.append(UserObject(json: entry))
      }
      dispatch_async(dispatch_get_main_queue(),{
        self.tableView.reloadData()
      })
    } else {
      print("error \(error)")
    }
  }
    task.resume()
  }
}

关于ios - UITableView创建空白单元格,而不是JSON中的任何值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38822923/

10-11 22:32