8
我正在使用Swift創建一個應用程序。 我有一個UITableView
,我用來自數據庫的一些數據填充。當用戶點擊一個單元格時,我想觸發一個動作。當用戶點擊一個單元格時觸發一個動作
我做了什麼:
var array: [String] = ["example"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell(style :UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell.textLabel.text = array[indexPath.row]
cell.tag = indexPath.row
cell.targetForAction("getAction:", withSender: self)
return cell
}
func getAction(sender:UITableViewCell)->Void {
if(sender.tag == 0) {
println("it worked")
}
}
我試着去適應從另一篇文章一個解決方案,但我這樣做是肯定不對的。謝謝你在前進
謝謝,這是完美的! – soling 2014-10-30 12:27:12
不要忘記實現'UITableViewDelegate'協議來獲得'didSelectRowAtIndexPath'方法 – LynAs 2017-01-12 10:31:18