当我快速单击viewForFooterInSection上的按钮时如何获取单元格数据

问题描述

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

我的代码就是这样

在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值。 预先感谢!

解决方法

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

var acceptButtonTapped: ((String?) -> ())?

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

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

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

通常将用户交互放置在单元格中。点击后,点击的单元格通过回调通知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
}
,

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

替换:

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

使用:

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...