title: Reuse item inside a row when scrolling
body: <p>I have <strong>collectionView</strong> inside <strong>tableView</strong>. <strong>collectionView</strong> need to horizontal scroll image, <strong>tableView</strong> for vertical scroll posts. When I had 3 <strong>rows</strong> I had no problems, but when i create 4 <strong>rows</strong> i have a problem with scrolling <strong>items</strong> inside rows. If I start scrolling on 4 <strong>row</strong>, scrolling is repeated on <strong>row</strong> 1 and the same thing if i start scrolling on 1 <strong>row</strong> scrolling is repeating on <strong>row</strong> 4.</p>
  
  <p>What could be the problem and how to solve it? May be</p>
  
  <p>Can check .gif file. I start on 1 <strong>row</strong> on name "<strong>Oko</strong>" and if i scrolling down on 4 <strong>row</strong> and scroll right <strong>collectionCell</strong> and <strong>return</strong> on 1 <strong>row</strong> i see next image name "City", but there must be name "<strong>Oko</strong>"</p>
  
  <p>My code:</p>
  
  <p><strong>ViewController:</strong></p>
  
  <pre><code>class PhotoStudiosViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
  
      @IBOutlet weak var tableView: UITableView!
  
      var theStudios: [Studio] = [] 
      var filteredStudios: [Studio] = [] 
  
      var studiosRef: DatabaseReference!
  
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(true)
  
          tableView.estimatedRowHeight = 475
          tableView.rowHeight = UITableViewAutomaticDimension
  
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          studiosRef = Database.database().reference(withPath: "PhotoStudios1")
  
          studiosRef.observe(.value, with: { (snapshot) in
  
              for imageSnap in snapshot.children {
  
                  let studioObj = Studio(snapshot: imageSnap as! DataSnapshot)
  
                  self.theStudios.append(studioObj)
  
              }
  
              self.tableView.reloadData()
  
          })
  
      }
      // MARK: - TableView
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
  
          if searchController.isActive &amp;&amp; searchController.searchBar.text != "" {
  
              return filteredStudios.count
  
          }
  
          return theStudios.count
  
      }
  
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! PhotoStudiosTableViewCell
  
          cell.currentPageNumber.text = "1/\(theStudios[indexPath.row].halls.count)"
  
          if searchController.isActive &amp;&amp; searchController.searchBar.text != nil {
              cell.theHalls = filteredStudios[indexPath.row].halls
          } else {
              cell.theHalls = theStudios[indexPath.row].halls
          }
  
          cell.nameLabel.text = theStudios[indexPath.row].studioName
  
          cell.addressLabel.text = theStudios[indexPath.row].studioAddress
  
          cell.logoLabel.sd_setImage(with: URL(string: theStudios[indexPath.row].studioLogo))
  
          cell.didSelectAction = {
  
              (innerPath) in
  
              self.showDetailsView(indexPath, cellPath: innerPath)
  
          }
  
          return cell
  
      }    
  </code></pre>
  
  <p><strong>TableViewCell:</strong></p>
  
  <pre><code>class PhotoStudiosTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout {
  
      @IBOutlet weak var button: UIButton!
      @IBOutlet weak var logoLabel: UIImageView!
      @IBOutlet weak var nameLabel: UILabel!
      @IBOutlet weak var addressLabel: UILabel!
      @IBOutlet weak var collectionView: UICollectionView!
      @IBOutlet weak var currentPageNumber: UILabel!
  
      var didSelectAction: ((IndexPath) -&gt; ())?
  
      var theHalls: [Hall] = [] {
          didSet {
              collectionView.reloadData()
          }
      }
  
      var lastContentOffset = CGPoint.zero 
  
      override func prepareForReuse() {
          super.prepareForReuse()
  
          resetCollectionView()
  
      }
  
      override func awakeFromNib() {
          super.awakeFromNib()
  
          currentPageNumber.layer.zPosition = 2
          currentPageNumber.layer.cornerRadius = 15.0
          currentPageNumber.clipsToBounds = true    
  
      }
  
      func resetCollectionView() {
          guard !theHalls.isEmpty else { return }
          theHalls = []
          collectionView.reloadData()
      }
  
      // MARK: - CollectionView
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
  
          return theHalls.count
  
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! PhotoStudiosCollectionViewCell2
  
          cell.hallName.text = theHalls[indexPath.item].hallName
  
          cell.priceLabel.text = theHalls[indexPath.item].hallPrice
  
          cell.metrslabel.text = theHalls[indexPath.item].hallMetrs
  
          cell.photoStudioImage.sd_setImage(with: URL(string: theHalls[indexPath.item].hallImage))
  
          return cell
  
      }
  
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  
          didSelectAction?(indexPath)
  
      }
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/6vjHL.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6vjHL.gif" alt="git image"></a></p>