Wizzardzz
Wizzardzz

Reputation: 841

Access to array's indexPath in a function Swift

I am trying to access an array's indexPath inside a function to update this array's data but I don't know how to pass the indexPath as a parameter (espacially what to pass when calling) to the function or if this is even the solution.

I included cellForRowAt to illustrate how this function access indexPath.

var cryptosArray: [Cryptos] = []

extension WalletTableViewController: UITableViewDelegate, UITableViewDataSource, CryptoCellDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let crypto = cryptosArray[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell
        cell.setCrypto(crypto: crypto)
        cell.delegate = self
        cell.amountTextField.delegate = self

        return cell
    }

    func cellAmountEntered(_ walletTableViewCell: WalletTableViewCell) {

         if walletTableViewCell.amountTextField.text == "" {
            return
        }
        let str = walletTableViewCell.amountTextField.text

        let crypto = cryptosArray[indexPath.row] //<---- How to do that?

        crypto.amount = walletTableViewCell.amountTextField.text

        //Then update array's amount value at correct index


        walletTableViewCell.amountTextField.text = ""

    }


}

Upvotes: 2

Views: 4993

Answers (2)

Milan Nos&#225;ľ
Milan Nos&#225;ľ

Reputation: 19737

Instead of hacking something, simply ask the tableView to tell you the indexPath of the given cell:

// use indexPath(for:) on tableView
let indexPath = tableView.indexPath(for: walletTableViewCell)

// then you can simply use it
let crypto = cryptosArray[indexPath.row]

UITableView.indexPath(for:) documentation says:

Returns an index path representing the row and section of a given table-view cell.

And this is exactly what you want, you don't want to hack the indexPath to the cell. indexPath should be taken care of by the tableView, not the cell. Ideally, cell should be completely oblivious of its indexPath.

Always try to use the standard way to solve your problems. In general, when you are trying to solve something, I would recommend you to first look at the documentation of the UITableView, there are many useful methods there.

Upvotes: 6

mohsen
mohsen

Reputation: 5058

if you want to get index path.row when user clicked on cell , you should get index path.row when user clicked and then use this to your func

for ex :

var indexrow : int = 0
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       // table cell clicked
       indexrow = indexPath.row
    }

func cellAmountEntered(_ walletTableViewCell: WalletTableViewCell) {

     if walletTableViewCell.amountTextField.text == "" {
        return
    }
    let str = walletTableViewCell.amountTextField.text

    let crypto = cryptosArray[indexrow] 

    crypto.amount = walletTableViewCell.amountTextField.text

    //Then update array's amount value at correct index


    walletTableViewCell.amountTextField.text = ""

}

Upvotes: 1

Related Questions