1

I have a UICollectionView with images. I implemented a SideMenu that contains a TableView when I select an item in the SideMenu I want it to automatically scroll to a specific row in the CollectionView

What do I do in didSelectRowAt?

Here is my full ViewController Class below

import SideMenu
import UIKit

class ViewController: UIViewController {
    var menu: SideMenuNavigationController?
    
    @IBOutlet weak var collectionView: UICollectionView!
    var imgArr = ["1","2","3","4","5","6","7","8","9","10"]
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        menu = SideMenuNavigationController(rootViewController: MenuListController())
        menu?.leftSide = true
        SideMenuManager.default.leftMenuNavigationController = menu
        SideMenuManager.default.addPanGestureToPresent(toView: self.view)
    }
    
    @IBAction func didTapMenu(){
        present(menu!, animated: true)
    }

}


extension ViewController : UICollectionViewDataSource, UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imgArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        var cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? DataCollectionViewCell
        cell?.img.image = UIImage(named:imgArr[indexPath.row])
        return cell!
    }
    
}

class MenuListController:UITableViewController{
    
    var items = ["First", "Second"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UITableView.self, forCellReuseIdentifier: "cellmenu")
        
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellmenu", for: indexPath)
        cell.textLabel?.text = items[indexPath.row]
        return cell
    }
      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("selected " + items[indexPath.row])
        // WHAT DO I DO HERE????
       
       
        
    }
    
}

enter image description here

user1163234
  • 2,407
  • 6
  • 35
  • 63
  • 1
    Does this answer your question? [UICollectionView auto scroll to cell at IndexPath](https://stackoverflow.com/questions/15985574/uicollectionview-auto-scroll-to-cell-at-indexpath) – Bohdan Savych Jun 17 '21 at 16:51
  • 1
    `collectionView.scrollToItem(at: IndexPath, at: UICollectionView.ScrollPosition.top, animated: true)` – CSmith Jun 17 '21 at 18:24
  • How can I access collectionView from MenuListController ? – user1163234 Jun 17 '21 at 19:59

1 Answers1

2

In IndexPath, Give the targeted cell's row and section.

collectioView.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
jignesh
  • 131
  • 1
  • 4