Ios 当用户单击swift中的单元格时触发操作
我正在使用Swift创建一个应用程序。 我有一个Ios 当用户单击swift中的单元格时触发操作,ios,uitableview,swift,Ios,Uitableview,Swift,我正在使用Swift创建一个应用程序。 我有一个UITableView,我用数据库中的一些数据填充它。当用户点击一个单元格时,我想触发一个动作 我所做的: var array: [String] = ["example"] func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } func
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")
}
}
我试图从另一篇文章中改编一个解决方案,但我做得绝对错误。提前感谢您实施
UITableViewDelegate
并使用didSelectRowAtIndexPath
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
您可以使用
indexPath
从您的收藏中获取物品,并在Swift 4中执行您的操作,我相信该方法已经发生了微小的变化:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// your code
}
谢谢,太好了!不要忘记实现
UITableViewDelegate
协议以获取didselectrowatinexpath
方法