这是我使用SWIFT语言的第一天,我试图用一些数据填充表格视图,一切似乎工作正常,但我想让我的表格视图可点击并打印出来的ID点击了该项目,但它似乎无法正常工作我没有收到任何错误。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var categories = [Category]()
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = categories[indexPath.row].Label
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
}
override func viewDidLoad() {
super.viewDidLoad()
let reposURL = NSURL(string: "http://myserver.com/file.json")
// 2
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
if let json = NSJSONSerialization.JSONObjectWithData(JSONData, options: nil, error: nil) as? NSDictionary {
// 4
if let reposArray = json["List"] as? [NSDictionary] {
// 5
for item in reposArray {
categories.append(Category(json: item))
}
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}