2016-08-08 1 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

を、注意してください。

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

あなたのGet JSON機能は

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

を削除する必要があります。私はちょうど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'を保存すると、 – fscore

関連する問題