繁体   English   中英

如何在Swift中点击表格视图的单元格

[英]How to make a click on cell of a Table View in Swift

这是我使用很棒的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.
}


}

似乎您已经忘记设置UITableViewDelegate了:

override func viewDidLoad() {
  super.viewDidLoad()
  self.tableView.delegate = self

}

如果尚未在代码中将tableView定义为变量。 您可以使用情节提要定义您的委托和数据源。

故事板

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM