0

I have a class which is called InvoicesViewController "first image"

in that class there are cells that come from the API and each cell has a Label which represents a price of an item and a button near that price which segues you to PayViewController "second image"

and now i want that the UITextField in the PayViewController "second image" to be filled with the selected cell's label price.

I hope I am clear and someone guides me to the appropriate answer since i can't wrap my head around this one :)

When user clicks the button Pay a .popover is introduced

This popover's UITextField needs to be filled with the selected cell's price label

This is my Code of the tableView :

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

    let data = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
        
    return data?.count ?? 0
}


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "invoicesCell", for: indexPath) as? invoicesModel else { return UITableViewCell() }
    
    let currentNotifications = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
    let currentInvoices = currentNotifications![indexPath.row]
    

    cell.mainPriceLabelInvoices.text = "€\(currentInvoices.priceWithVAT ?? 0.00)"

    return cell
}
Lis1
  • 13
  • 5
  • Actually you need a closure passed in `cellForRowAt` which is called by the button in the cell to identify the row (as described [here](https://stackoverflow.com/questions/43692747/delete-row-uitableview-index-issue/43699231#43699231)). The price information is in the data source array. By the way the tableview data source methods are called quite often so something like `notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices` is very expensive. And you are using too many optionals. – vadian Jun 06 '22 at 14:33
  • @vadian please guide me , I am new and we all have been here so if you could please provide an answer to be more clear of the identification of the row because I can somehow understand you – Lis1 Jun 06 '22 at 14:36

2 Answers2

0

you can use navigation code for move to the next page and pass the value with navigation.

let storyBoard : UIStoryboard = UIStoryboard(name: "PayViewController", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "PayViewController") as! PayViewController
nextViewController.txtfield.text = value
self.present(nextViewController, animated:true, completion:nil)
Arjun
  • 126
  • 4
  • Thanks a lot Arjun for the answer, can you be more specific were to put your code please since I am new – Lis1 Jun 06 '22 at 14:28
  • you should use above code when you want to go to next screen. This code is call on button action. – Arjun Jun 06 '22 at 15:43
0

At first, create a var in PayViewController like this

var price : Double = 0.0

Now in viewDidLoad() method write down the below code

self.textField.text = "€\(price)"

then, you have to write the following code in cellForRowAt func of tableView in InvoicesViewController

cell.editBtn.addTarget(self, action: #selector(buttonPayTapped(_:)), for: .touchUpInside)

Now add the following function

@objc func buttonPayTapped(_ sender:UIButton) {
    guard let indexPath = tableView.indexPathForView(sender) else {return }
    
    let vc = self.storyBoard?.instantiateViewController(withIdentifier: "PayViewController") as! AddCategoriesVC
    
    let currentNotifications = notifications.Result![changeCustomerKey.DefaultsKeys.keyTwo].properties?[0].invoices
    let currentInvoices = currentNotifications![indexPath.row]
    
    // This is to transfer the selcted Data to Payment page
    vc.price = currentInvoices.priceWithVAT ?? 0.00
    self.present(vc, animated:true, completion:nil)
    
}

Add the following extension to get the exact indexPath

extension UITableView {
    func indexPathForView(_ view: UIView) -> IndexPath? {
        let center = view.center
        let viewCenter = convert(center, from: view.superview)
        let indexPath = indexPathForRow(at: viewCenter)
        return indexPath
    }
}
  • Thanks for your correct answer, just one thing you are calling is in did select when the user just needs to click the Pay button and then be segued to the other VC – Lis1 Jun 07 '22 at 11:49
  • Hi @Lis1 , I have updated my code for you. – Shivam Garg Jun 08 '22 at 17:16
  • I get this error on the line of guard let indexPath = tableView.indexPathForView , Reference to member 'indexPathForView' cannot be resolved without a contextual type – Lis1 Jun 09 '22 at 08:29
  • You have to add the TableView extension in your project, with the function indexPathForView() which I mentioned in the answer – Shivam Garg Jun 09 '22 at 10:40
  • Already did buddy but still get that error idk why mate – Lis1 Jun 09 '22 at 11:38