title: Expand one row at a time in UITableView Swift
body: <p>I am working on expanding row when tapped on table view row. My logic is simple I am adding an extra row when tapped on visible row(i.e Expanding) and I delete it i.e remove/collapse the added row when tapped on expanded visible row. Now I have 4 custom cells with separate XIB's i.e A, B, C, D and all of them are contained in one Class. Now the issue goes something like this consider I expand cell A at row 0 and next row is also of type A then the instance goes to the second open cell and when I tap on first open cell it crashes. Now I have 2 solutions first is to show only one cell at a time and collapse all the remaining open cell if any or else second is solve the crash issue that doesn't get the cells instance. I have tried to solve the first issue by creating </p>
  
  <p><code>let indexPath : IndexPath = IndexPath(row: sender.view!.tag, section: 0)
  self.expansionCell = self.myTableView.cellForRow(at: indexPath) as! CustomCell</code></p>
  
  <p>But still it crashes.</p>
  
  <p>Second one I tried the below </p>
  
  <pre><code>public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
  
      if isExpanded == true
      {
          print("Expanded Row",self.expandedIndex)
         //Function to contract Cell which holds old expanded cell index
         self.contractExpandedCell(tableView: tableView, index: self.expandedIndex)
      }
  
      //Setting Selected index path
      self.selectedIndexPath = indexPath as NSIndexPath
  
      if myArray[indexPath.row] != nil {
          let cell = myTableView.cellForRow(at: indexPath) as! CustomCell
  
          // If user clicked last cell, do not try to access cell+1 (out of range)
          if(indexPath.row + 1 &gt;= (myArray.count)) {
              DispatchQueue.main.async {
                  cell.forwardArrowImage.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
              }
              self.isExpanded = true
  
              //Function to expand Cell
              expandCell(tableView: tableView, index: indexPath.row)
  
          }
          else {
              // If next cell is not nil, then cell is not expanded
              if(myArray[indexPath.row+1] != nil) {
                  DispatchQueue.main.async {
                      cell.forwardArrowImage.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI_2))
  
                  }
                  self.isExpanded = true
                  self.expandCell(tableView: tableView, index: indexPath.row)
  
              } else {
                  // Close Cell (remove ExpansionCells)
                  DispatchQueue.main.async {
                      cell.forwardArrowImage.transform = CGAffineTransform(rotationAngle: CGFloat(180.0 * M_PI))
                  }
                  self.isExpanded = false
                  contractCell(tableView: tableView, index: indexPath.row)
  
              }
          }
      }
  
  }
   //Function to expand cell  
   private func expandCell(tableView: UITableView, index: Int) {
      // Expand Cell (add ExpansionCells)
      if (myArray[index]?.title) != nil {
          myArray.insert(nil, at: index + 1)
          self.expandedIndex = index + 1
          questionAnswerTable.insertRows(at: [IndexPath(row: index + 1, section: 0)], with: .top)
      }
  }
  
  //Function to contract cell  
  private func contractCell(tableView: UITableView, index: Int) {
      if (myArray[index]?.title) != nil {
          myArray.remove(at: index + 1)
          questionAnswerTable.deleteRows(at: [IndexPath(row: index + 1, section: 0 )], with: .top)
      }
  
  }
  </code></pre>
  
  <p>Even this doesn't work smoothly as when a row is added the row count changes and if I try to expand the next cell it opens the third one.</p>
  
  <p>Any one solution will work.
  Please help guys. TIA</p>