gpt4 book ai didi

ios - 如何在 Swift 中点击表格 View 的单元格

转载 作者:行者123 更新时间:2023-11-29 01:46:51 25 4
gpt4 key购买 nike

这是我使用令人敬畏的 SWIFT 语言的第一天,我正在尝试用一些数据填充 TableView ,一切似乎都工作正常,但我想让我的 TableView 可单击并打印单击的项目的 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 定义为变量。您可以使用 Storyboard定义委托(delegate)和数据源。

storyboard

关于ios - 如何在 Swift 中点击表格 View 的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31810511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com