1

enter image description here

Issue: I have a table view in complaintController which has "open" buttons in it. Whenever I press on the open button I segue to DetailComplaintViewController with the data of that row, but when I go back to ComplaintController and select a different button I am presented with the same data from previous selection.

NOTE - I have created a segue from button in cell of tableView.

This is the code I am using to segue from ComplaintController to DetailComplaintController.

var passRow = 0
        public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

            let billCell = tableView.dequeueReusableCell(withIdentifier: "complaintCell") as! ComplaintTableViewCell
            billCell.openBtn.tag = indexPath.row
            billCell.openBtn.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
            return billCell

    }

             func btnClicked(sender: UIButton) {
                    passRow = sender.tag
                    print("Position......\(sender.tag)")
                }

                override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                    let rVC = segue.destination as? DetailComplaintViewController
                    rVC?.issueType = self.complaintListArray[passRow].issueType
                    rVC?.issuedescription = self.complaintListArray[passRow].description
                    rVC?.issuedate = self.complaintListArray[passRow].issueDate
                }
Ojas Chimane
  • 104
  • 10

1 Answers1

1

The problem is that the prepare for segue will be called before your btnClicked function and thats why you are not getting the correct data.

A quick fix for your situation would be to get the button tag inside the prepare for segue method like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    guard let button = sender as? UIButton else {
        print("Segue was not called from button")
        return
    }
    let row = button.tag
    let rVC = segue.destination as? DetailComplaintViewController
    rVC?.issueType = self.complaintListArray[row].issueType
    rVC?.issuedescription = self.complaintListArray[row].description
    rVC?.issuedate = self.complaintListArray[row].issueDate
}

Other option is to remove the segue from the button and create it on the view controller and programatically perform that segue inside your btnClicked method which is explained in this answer

Swifty
  • 3,730
  • 1
  • 18
  • 23