ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?

ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?,第1张

概述参见英文答案 > Issue Detecting Button cellForRowAt                                    3个 >             swift: how to get the indexpath.row when a button in a cell is tapped?                                  参见英文答案 > Issue Detecting Button cellForRowAt                                    3个
>             swift: how to get the indexpath.row when a button in a cell is tapped?                                    14个
我在UItableVIEwCell中有一个 *** 作按钮,我想检测按钮的按下时间以及VIEwController中按下的单元格编号,以便在VIEwController.swift中创建音频播放列表. @H_403_9@

@H_403_9@我已经陷入这个问题一段时间了,我真的很赞赏你的建议.这是代码.

@[email protected]

@H_403_9@

import UIKitclass VIEwController: UIVIEwController,UItableVIEwDelegate,UItableVIEwDataSource {    @IBOutlet weak var tableVIEw: UItableVIEw!    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        tableVIEw.delegate = self        tableVIEw.dataSource = self        tableVIEw.register(UINib(nibname: "Cell",bundle: nil),forCellReuseIDentifIEr: "cell")    }    func tableVIEw(_ tableVIEw: UItableVIEw,numberOfRowsInSection section: Int) -> Int {        return 3    }    func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {        let cell = tableVIEw.dequeueReusableCell(withIDentifIEr: "cell") as! Cell        return cell    }}
@[email protected]

@H_403_9@

import UIKitclass Cell: UItableVIEwCell {    @IBOutlet weak var button: UIbutton!    @IBAction func buttonpressed(_ sender: Any) {        ***[Code to send the pressed cell's number to VIEwController]***    }}
解决方法 你可以选择一个老式的代表模式.这样做的好处是不会将视图控制器与单元格耦合.不要忘记让你的代表弱,以避免保留周期. @H_403_9@

@H_403_9@您可以从表视图中找到单元索引路径. (我假设按单元格编号表示索引路径)

@H_403_9@

protocol CellDelegate: class {    func dIDTap(_ cell: Cell)}class Cell: UItableVIEwCell {    weak var delegate: CellDelegate?    @IBAction func buttonpressed(_ sender: Any) {        delegate?.dIDTap(self)    }}class VIEwController: UIVIEwController,CellDelegate {    func tableVIEw(_ tableVIEw: UItableVIEw,cellForRowAt indexPath: IndexPath) -> UItableVIEwCell {        let cell = ...        cell.delegate = self        return cell    }    func dIDTap(_ cell: Cell) {        let indexPath = self.tableVIEw.indexPath(for: cell)        // do something with the index path    }}
总结

以上是内存溢出为你收集整理的ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?全部内容,希望文章能够帮你解决ios – Swift – 如何从ViewController中按下UItableViewCell中的动作按钮?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063539.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存