2014-11-25 2 views
0

에서 내 코드 데이터를 표시하지 않습니다표보기 JSON

import UIKit 

class HomeVCHome: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet weak var usernameLabel: UILabel! 
@IBOutlet weak var tableView: UITableView! 

var names: [String] = [] 
var contacts: [String] = [] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 

    let url=NSURL(string:"http://mysite/json.aspx")! 
    let allContactsData=NSData(contentsOfURL:url) 

    var allContacts: AnyObject! = NSJSONSerialization.JSONObjectWithData(allContactsData!, options: NSJSONReadingOptions(0), error: nil) 


    if let json = allContacts as? Array<AnyObject> { 

     print(json) 
     for index in 0...json.count-1 { 

      let contact : AnyObject? = json[index] 
      print(contact) 

      let collection = contact! as Dictionary<String, AnyObject> 
      print(collection) 

      print(collection["name"]) 

      let name : AnyObject? = collection["name"] 
      let cont : AnyObject? = collection["cont"] 

      names.append(name as String) 
      contacts.append(cont as String) 
     } 
    } 
    println(names) 
    println(contacts) 
    tableView.reloadData() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { 
    return self.names.count; 
} 
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) { 
    println("You selected name : "+names[indexPath.row]) 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell 
    println("ok 1") 
    if !(cell != nil) { 
     cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "cell") 
    } 
    println("ok 2") 
    cell?.textLabel.text=self.names[indexPath.row] 
    cell?.detailTextLabel?.text = self.contacts[indexPath.row] 
    println("ok 3") 
    return cell! 
} 

} 내가 실행하려고

을, 그리고 난 테이블보기에서 단지 빈 ...있는 tableView 내 데이터를 볼 수 없습니다 및 다른 코드를 시도하지만 동일한 결과 (표보기 비어 있음) ... 어떻게해야합니까? 나는 그것의 내 실수 코드를 모르거나 내 시뮬레이터는 그런 문제가 ...

+0

어쩌면 당신이 그것을 디버깅해야합니다. –

+1

ypo가 테이블 뷰의 대리인 및 데이터 소스를 설정 했습니까? –

+0

@Virussmca UITableViewDelegate, UITableViewDataSource를 설정하려고 시도했지만 동일한 결과가 나타납니다. – Noob

답변

2

PLS는 방법을 변경 시도 :

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as UITableViewCell 
    cell.textLabel.text=self.names[indexPath.row] 
    cell.detailTextLabel?.text = self.contacts[indexPath.row] 
    return cell 
}