【问题标题】:How to get Cell data when I click on button which is on viewForFooterInSection in swift当我快速单击 viewForFooterInSection 上的按钮时如何获取单元格数据
【发布时间】:2020-08-16 14:55:32
【问题描述】:

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

我的代码是这样的

In viewForFooterInSection

    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
        }

点击按钮

 @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 值。 提前致谢!

【问题讨论】:

    标签: swift uitableview uitableviewsectionheader


    【解决方案1】:

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

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

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

    footerview.acceptButtonTapped = { text in
        print(text)
    }
    

    【讨论】:

    • 当我返回 return footerview.contentView 时,您的代码不起作用
    • 为什么返回footerview.contentview?
    • 因为在 textview 完成点击页脚视图消失
    • 为什么会消失?按钮操作和viewForFooterInSection 方法中发生了什么其他事情?
    【解决方案2】:

    使用footerView(forSection:) 方法代替cellForRow(at:)

    替换:

    let cell: TimeSheetFooterTableViewCell = self.tblview.cellForRow(at: index) as! TimeSheetFooterTableViewCell
    

    与:

    let footerView = tblview.footerView(forSection: sender.tag) as! TimeSheetFooterTableViewCell
    

    【讨论】:

    • 向我显示此错误。调用中的参数标签不正确(有 'forSection:',预期为 'at:')
    • 我错过了一点。再试一次,我刚刚更新了方法。
    • 从 'UITableViewHeaderFooterView?' 转换到不相关的类型“TimeSheetFooterTableViewCell”总是失败
    • 您能否检查一下我已经为 TimeSheetFooterTableViewCell 创建了 .xib
    • TimeSheetFooterTableViewCell 应该是 UITableViewHeaderFooterView 类型。否则它将无法正常工作。确保class TimeSheetFooterTableViewCell: UITableViewHeaderFooterView {
    【解决方案3】:

    通常是放置在单元格中的用户交互。并且点击后,被点击的单元格通过回调通知tablewView。

    这是一个单元格代码:

       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 委托中的页脚功能:

    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
    }
    

    【讨论】:

    • 如何在按钮点击时获取 textview 的数据?
    • 我更新了答案。如果 lblTimeSheetFooterID 是您的 UITextField 或 UILabel 它应该适合您
    • 不工作这个方法没有在按钮点击footerview.btnDidTap = { comment in print("section number: (section) DID TAP!") }
    • 你能分享你现有的代码吗?我想你忘记了我的例子中的 awakeFromNib 函数。
    • 你的 TimeSheetFooterTableViewCell 也应该是 UITableViewHeaderFooterView 的子类。换句话说,它不应该工作
    猜你喜欢
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    相关资源
    最近更新 更多