栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

UITableViewCell按钮与动作

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

UITableViewCell按钮与动作

我正在使用UITableViewCell的子类中的单元委托方法解决此问题。

快速概述:

1) 创建一个协议

protocol YourCellDelegate : class {    func didPressButton(_ tag: Int)}

2) 子类化 您的 子类

UITableViewCell
(如果尚未这样做):

class YourCell : UITableViewCell{     var cellDelegate: YourCellDelegate?         @IBOutlet weak var btn: UIButton!    // connect the button from your cell with this method    @IBAction func buttonPressed(_ sender: UIButton) {        cellDelegate?.didPressButton(sender.tag)    }  ...}

3) 您的视图 控制器 符合

YourCellDelegate
上面实现的协议。

class YourViewController: ..., YourCellDelegate {  ... }

4) 在定义了单元之后(用于重用), 设置一个委托

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! YourCellcell.cellDelegate = selfcell.btn.tag = indexPath.row

5) 在同一控制器(您实现的UITableView委托/数据源在哪里)中, 放置来自

YourCellDelegate
protocol
的方法

func didPressButton(_ tag: Int) {     print("I have pressed a button with a tag: (tag)")}

现在,您的解决方案不再取决于标签/数字。您可以根据需要添加任意数量的按钮,因此无论您要安装多少个按钮,都可以通过委托获得响应。

此协议代理解决方案在iOS逻辑中是首选,它可用于表单元格中的其他元素,例如

UISwitch
UIStepper
等等。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413212.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号