繁体   English   中英

Swift 3 button.addTarget不适用于所有表单元格

[英]Swift 3 button.addTarget not working on all table cells

我创建了一个应用程序,允许用户存储针对评论的各种语音记录。 当我显示此表时,数据将使用以下代码填充:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let row = (indexPath as NSIndexPath).item
    let cell = tableView.dequeueReusableCell(withIdentifier: "voiceRecordingCell", for: indexPath) as! VoiceRecordingTableViewCell
    let voiceRecording = self.voiceRecordings[row] as! NSDictionary

    let isoFormatter = DateFormatter()
    isoFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'.000Z'"
    let createdAt = isoFormatter.date(from: voiceRecording["created_at"] as! String)
    self.recordingIndexPaths.insert(indexPath, at: row)

    cell.recording = voiceRecording
    cell.date.text = getDateFormatter("dd-MM-y").string(from: createdAt!)
    cell.time.text = getDateFormatter("HH:mm").string(from: createdAt!)
    cell.length.text = voiceRecording["length"] as? String
    cell.location.text = voiceRecording["location"] as? String

    let audioPlayerController = self.storyboard?.instantiateViewController(withIdentifier: "AudioPlayerController") as! AudioPlayerController

    audioPlayerController.voiceRecording = voiceRecording

    cell.audioPlayer.addSubview(audioPlayerController.view)
    self.addChildViewController(audioPlayerController)
    audioPlayerController.didMove(toParentViewController: self)

    cell.deleteRecordingButton.tag = row
    cell.deleteRecordingButton.addTarget(self, action: #selector(deleteRecordingPressed(_:)), for: .touchUpInside)

    return cell
}

单元格似乎都正确呈现,但是对于最初未随页面呈现的单元格,我必须向下滚动以查看,当我单击音频播放器控件或deleteRecordingButton上的按钮时,没有任何反应,好像没有设置addTarget 设置按钮的代码被调用并且不会产生错误,只是不应用于这些按钮。

最初显示在屏幕上的按钮具有正确的操作,并且都可以正常工作,所以我知道这是可行的。

我真的不知道是什么原因造成的。 我曾尝试搜索google和stackoverflow,但未找到任何内容。 对此将提供任何帮助。

---------------更新-----------

我只是尝试在其中设置一些断点

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

这也只能在顶部的2个单元格上调用,如果在横向,则在顶部的单元格上调用!

由于单元一直在重复使用,因此参考丢失了。 尝试这样的事情:

class ButtonTableViewCell: UITableViewCell {

    typealias TapClosure = () -> Void

    var buttonTapped: TapClosure?

    @IBAction func buttonTouchUpInside(sender: UIButton) {
        buttonTapped?()
    }

}

extension TestViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as! ButtonTableViewCell

        cell.buttonTapped = {
            print("Button tapped")
        }

        return cell
    }

}

还有另一个小费。 切勿在cellForRowAtIndexPath中初始化DateFormatter 而是在viewDidLoad或静态结构中创建它以供重用。

终于我明白了...

发生这种情况的原因是,我使用的是带有滚动视图的滚动视图,而不使用表的本机滚动功能来上下滚动表。

使用表格滚动功能时,将按钮应用到将动作带入视图中的过程。 这由func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {方法处理。 但是,当您按照我原先的方式进行操作时,它最初将渲染所有单元,而屏幕外的单元将无法正常工作!

以这种方式工作有点烦人,但至少我现在知道这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM