Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.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 在tableView中为按钮添加目标_Ios_Swift_Uitableview_Swift3 - Fatal编程技术网

Ios 在tableView中为按钮添加目标

Ios 在tableView中为按钮添加目标,ios,swift,uitableview,swift3,Ios,Swift,Uitableview,Swift3,我正在尝试为我的tableView中的某些项目添加下载按钮。我已经创建了自定义单元格类,并添加了标签和按钮出口,所有的东西都在显示信息,甚至按钮都在显示它应该在哪里 我试图添加目标,但它没有任何作用。我需要将行索引传递给buttonClicked函数,还是应该在自定义单元格类中创建此函数,然后以某种方式执行该操作?我想知道这方面的最佳做法 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&g

我正在尝试为我的tableView中的某些项目添加下载按钮。我已经创建了自定义单元格类,并添加了标签和按钮出口,所有的东西都在显示信息,甚至按钮都在显示它应该在哪里

我试图添加目标,但它没有任何作用。我需要将行索引传递给buttonClicked函数,还是应该在自定义单元格类中创建此函数,然后以某种方式执行该操作?我想知道这方面的最佳做法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "PlaylistCell", for: indexPath) as! PlaylistTableViewCell

        let playlist = self.playlists?[indexPath.row]

        cell.titleLabel.text = playlist?.getTitle()


        if (playlist?.isOfflineAvailable())! {
            cell.downloadButton.isHidden = false
        } else {
            cell.downloadButton.isHidden = true
            cell.downloadButton.tag = indexPath.row
            cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
        }

        return cell
    }

    func buttonClicked(sender: UIButton) {
        let buttonRow = sender.tag
        print(buttonRow)
    }

我还尝试从选择器中删除sender:,但它不会更改功能。

为了处理视图控制器中的按钮回调,您有两种选择:

目标行动:

在cellForRow方法中添加目标操作,就像您所做的一样。您的代码可能不起作用,因为您在按钮应该可见时隐藏了它,不是吗

我想你需要更换这个

if (playlist?.isOfflineAvailable())! {
    cell.downloadButton.isHidden = false
} else {
    cell.downloadButton.isHidden = true
    cell.downloadButton.tag = indexPath.row
    cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
}
为此:

cell.downloadButton.isHidden = playlist?.isOfflineAvailable()
cell.downloadButton.tag = indexPath.row
cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
每次都应该更新标记,因为单元格在tableView中被重用,如果不在每次调用cellForRow时都更新标记,则很容易在调用回调时得到一个例子,但它的标记属于以前使用的单元格中的indexPath。我还将isHidden逻辑更改为相反的。我想当IsoflineAvailable返回true时,您应该隐藏按钮,对吗

委托模式


它在这里被描述了一百万次,在其他许多网站上也是如此。基本上,您定义一个单元协议,在控制器中实现它,并在按下按钮时从单元向其代理发送回调。您可以在中找到类似问题的更多详细信息。

为了处理视图控制器中的按钮回调,您有两个选择:

目标行动:

在cellForRow方法中添加目标操作,就像您所做的一样。您的代码可能不起作用,因为您在按钮应该可见时隐藏了它,不是吗

我想你需要更换这个

if (playlist?.isOfflineAvailable())! {
    cell.downloadButton.isHidden = false
} else {
    cell.downloadButton.isHidden = true
    cell.downloadButton.tag = indexPath.row
    cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
}
为此:

cell.downloadButton.isHidden = playlist?.isOfflineAvailable()
cell.downloadButton.tag = indexPath.row
cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
每次都应该更新标记,因为单元格在tableView中被重用,如果不在每次调用cellForRow时都更新标记,则很容易在调用回调时得到一个例子,但它的标记属于以前使用的单元格中的indexPath。我还将isHidden逻辑更改为相反的。我想当IsoflineAvailable返回true时,您应该隐藏按钮,对吗

委托模式


它在这里被描述了一百万次,在其他许多网站上也是如此。基本上,您定义一个单元协议,在控制器中实现它,并在按下按钮时从单元向其代理发送回调。您可以在中找到类似问题的更多详细信息。

您还可以将iAction添加到您的单元格中,并在那里调用委托方法:谢谢,我真的需要这个!不客气。事实上,目标行动法没有错。查看下面的答案了解详细信息。你也可以将iAction添加到你的手机中,并在那里调用委托方法:谢谢,我真的需要这个!不客气。事实上,目标行动法没有错。查看下面的答案以了解详细信息。