How can I get the index path.row of first UITableView in the Second UITableView using Swift3

1k views Asked by At

I have two UITableViews. The first UITableView contains an array of data, and the second is empty. How can I get the index path of the first table to use it in the second table to view different data? This is an example of what I want to do in the second table:

This is in the second table and like this function where I want to get the indexPath.row of first table.

  func loadData() {

      if indexPath.row == 0 { // (of first table)

      // add code

      } else if indexPath.row == 1 { // (of first table)

       // add code

     }
 }
2

There are 2 answers

4
Dheeraj D On

Implement didSelectRowAt

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    let selctedIndex = indexPath.row
    self.setIndexToSecondTableView(selctedIndex) //Create your function to get selected index of first table view
}
0
Meera On

Hope You get solution.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
        {
            if tableView.isEqual(firstTableView) {
               self.updateScecondTableView(index: IndexPath.row) //make a function to retrieve selected index from first table view
            } else {
                // acode
            }
        }

func updateScecondTableView(index:NSIndexPath)  {
        switch index.row {
        case 0:
            // add a code
            break

        default:
            // add a code
            break
        }
    }