Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 当用户单击swift中的单元格时触发操作_Ios_Uitableview_Swift - Fatal编程技术网

Ios 当用户单击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

我正在使用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")
        }
    }

我试图从另一篇文章中改编一个解决方案,但我做得绝对错误。提前感谢您

实施
UITableViewDelegate
并使用
didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

您可以使用
indexPath
从您的收藏中获取物品,并在Swift 4中执行您的操作,我相信该方法已经发生了微小的变化:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // your code
}

谢谢,太好了!不要忘记实现
UITableViewDelegate
协议以获取
didselectrowatinexpath
方法