首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在swift中点击viewForFooterInSection上的按钮时获取单元格数据

如何在swift中点击viewForFooterInSection上的按钮时获取单元格数据
EN

Stack Overflow用户
提问于 2020-08-16 22:55:32
回答 3查看 60关注 0票数 0

我是swift新手,当我点击viewForFooterInSection中的按钮时会遇到问题

我的代码是这样的

在viewForFooterInSection中

代码语言:javascript
运行
复制
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            let footerview = Bundle.main.loadNibNamed("TimeSheetFooterTableViewCell", owner: self, options: nil)?.first as! TimeSheetFooterTableViewCell
            
            let dictFeeStats = arrFinancialYears[section] as? [String:Any]
            footerview.lblTimeSheetFooterID.text = dictFeeStats?["staff_timesheet_id"] as? String            

            footerview.btnAccept.tag = section
            footerview.btnAccept.addTarget(self, action: #selector(btnAcceptClick), for: .touchUpInside)
            
            return footerview
        }

单击按钮时

代码语言:javascript
运行
复制
 @objc func btnAcceptClick(_ sender: UIButton)
    {
        let index = IndexPath(row: sender.tag, section: 0)
        let cell: TimeSheetFooterTableViewCell = self.tblview.cellForRow(at: index) as! TimeSheetFooterTableViewCell
        let comment = cell.lblTimeSheetFooterID.text
        
        print("buttonPressed ! \(sender.tag)")
    }

如何在注释变量中获取TimeSheetFooterTableViewCell值。提前感谢!

EN

回答 3

Stack Overflow用户

发布于 2020-08-16 23:17:29

您可以在TimeSheetFooterTableViewCell中添加一个接受字符串的闭包。当点击按钮时,使用文本视图的文本调用该闭包。

代码语言:javascript
运行
复制
var acceptButtonTapped: ((String?) -> ())?

@IBAction func btnAcceptClick(_ sender: UIButton) {
    acceptButtonTapped?(txtview.text)
}

tableView(_ tableView: UITableView, viewForFooterInSection中,从回调中获取文本。

代码语言:javascript
运行
复制
footerview.acceptButtonTapped = { text in
    print(text)
}
票数 1
EN

Stack Overflow用户

发布于 2020-08-16 23:12:01

通常是放在单元中的用户交互。并且在tap之后,tapped cell通过回调通知tablewView。

这是一个单元代码:

代码语言:javascript
运行
复制
   class TimeSheetFooterTableViewCell: UITableViewCell {
    
    var btnAccept: UIButton!
    var lblTimeSheetFooterID: UITextView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        btnAccept = UIButton()
        btnAccept.addTarget(self, action: #selector(btnAcceptClick), for: .touchUpInside)
    }
    
    //Callback for communication between cell and tableView
    var btnDidTap: ((String) -> Void)?
    
    @objc func btnAcceptClick(_ sender: UIButton) {
        btnDidTap?(lblTimeSheetFooterID.text ?? "")
    }
    
    

    
}

这是tableView委托中的页脚函数:

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerview = Bundle.main.loadNibNamed("TimeSheetFooterTableViewCell", owner: self, options: nil)?.first as! TimeSheetFooterTableViewCell
    
    let dictFeeStats = arrFinancialYears[section] as? [String: Any]
    
    footerview.btnDidTap = { comment in
        print("section number: \(section) DID TAP!")
    }
    
    return footerview
}
票数 0
EN

Stack Overflow用户

发布于 2020-08-16 23:18:17

请使用footerView(forSection:)方法而不是cellForRow(at:)

替换:

代码语言:javascript
运行
复制
let cell: TimeSheetFooterTableViewCell = self.tblview.cellForRow(at: index) as! TimeSheetFooterTableViewCell

通过以下方式:

代码语言:javascript
运行
复制
let footerView = tblview.footerView(forSection: sender.tag) as! TimeSheetFooterTableViewCell
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63438315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档