JSONからtableviewに値をロードしようとしています。ここに私のコードはありますが、何が分からないのか分かりません。空のセルのリストを表示しますが、値は表示しません。 UITableViewは空白のセルを作成し、JSONから値を取得しません
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()
}
}
は、あなたの非同期要求が完了した後、あなたは
tableview
の
reloadData
funcを呼び出す必要があります
print("count /(self.items.count)")
戻って正しい4が、
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//function does not show any print statements.
}
ありがとうございました!しかし、これは唯一のセルを印刷します!.textLabel?.text = user.nameしかし、私がセルを行う場合!.textLabel?.text = user.dateそれはtableView上で印刷されません..任意のアイデア? – fscore
JSONやユーザーモデルがわからないため、調査できません。 –
@KenanKarakecili user.dateは存在するので、 'cell!.textLabel?.text = user.name'をコメントアウトして' cell!.textLabel?.text = user.date'を保存すると、 – fscore