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'를 유지하면 작동하고 viceversa는 함께 있지만 둘 다 함께 작동하지 않습니다. – fscore