2016-08-08 3 views
0

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() 
    } 
} 

은 비동기 요청이 완료 후에는 tableviewreloadData FUNC 호출해야 print("count /(self.items.count)") 되돌아 올 4하지만

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
//function does not show any print statements. 

} 

답변

2

을 유의하시기 바랍니다.

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() 
} 

하고보기 didload에서

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

을 제거, 같은

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() 
    } 
} 
1

귀하의 가져 오기 JSON 기능이 있어야한다. web service call의 완성 처리기에 테이블보기를 다시로드하는 코드를 넣었습니다.

+0

감사합니다. 그러나이 셀만 인쇄합니다! .textLabel? .text = user.name하지만 셀을 수행하면! .textLabel? .text = user.date 그런 다음 tableView에서 인쇄하지 않습니다. 어떤 생각입니까? – fscore

+0

JSON 또는 사용자 모델을 모르기 때문에 조사 할 수 없습니다. –

+0

@KenanKarakecili user.date가 존재하므로 'cell! .textLabel? .text = user.name'을 주석으로 처리하고'cell! .textLabel? .text = user.date'를 유지하면 작동하고 viceversa는 함께 있지만 둘 다 함께 작동하지 않습니다. – fscore

관련 문제