0

I am new in swift and I am facing problem when I click on button which is in viewForFooterInSection

my code is like this

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
        }

On Button Click

 @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)")
    }

How can I get TimeSheetFooterTableViewCell value in comment variable. Thanks in Advance!

Mujtaba
  • 97
  • 1
  • 7

3 Answers3

1

You can add a closure in TimeSheetFooterTableViewCell that accepts a string. When button is tapped, call that closure with the textview's text.

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

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

In your tableView(_ tableView: UITableView, viewForFooterInSection, get the text from the callback.

footerview.acceptButtonTapped = { text in
    print(text)
}
Jithin
  • 913
  • 5
  • 6
0

usually the user interactions placed in the cell. And after tap, tapped cell informs the tablewView through callbacks.

This a cell code:

   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 ?? "")
    }
    
    

    
}

This is a footer function in tableView delegate:

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

Use footerView(forSection:) method instead of cellForRow(at:).

Replace:

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

With:

let footerView = tblview.footerView(forSection: sender.tag) as! TimeSheetFooterTableViewCell
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • showing me this error. Incorrect argument label in call (have 'forSection:', expected 'at:') – Mujtaba Aug 16 '20 at 15:21
  • I missed a bit. Try again, I've just updated the method. – Frankenstein Aug 16 '20 at 15:22
  • Cast from 'UITableViewHeaderFooterView?' to unrelated type 'TimeSheetFooterTableViewCell' always fails – Mujtaba Aug 16 '20 at 15:24
  • Can you please check I have created .xib for TimeSheetFooterTableViewCell – Mujtaba Aug 16 '20 at 15:29
  • `TimeSheetFooterTableViewCell` should be of type `UITableViewHeaderFooterView`. It won't work otherwise. Make sure `class TimeSheetFooterTableViewCell: UITableViewHeaderFooterView {` – Frankenstein Aug 16 '20 at 16:05
  • Can't I convert it into cell? – Mujtaba Aug 16 '20 at 16:09
  • It should have been possible. But `footView(forSection:)` creates an instance of `UITableViewHeaderFooterView` while `TimeSheetFooterTableViewCell` is not inherited from `UITableViewHeaderFooterView`. You see why this won't work here right? Why don't you make a separate xib with the same layout, only difference being the the class would be inherited from `UITableViewHeaderFooterView`. – Frankenstein Aug 16 '20 at 16:29