Auto scroll UITableview (with header section) when I clicked collectionview cell index

46 views Asked by At

I want to scroll till the last header section of a tableView. My code looks like this:

 var mobileBrand = [MobileBrand]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        mobileBrand.append(MobileBrand.init(brandName: "Apple", modelName: ["iPhone 5s","iPhone 6","iPhone 6s", "iPhone 7+", "iPhone 8", "iPhone 8+", "iPhone 11", "iPhone 11 Pro"]))
        mobileBrand.append(MobileBrand.init(brandName: "Samsung", modelName: ["Samsung M Series", "Samsung Galaxy Note 9", "Samsung Galaxy Note 9+", "Samsung Galaxy Note 10", "Samsung Galaxy Note 10 +"]))
        mobileBrand.append(MobileBrand.init(brandName: "Mi", modelName: ["Mi Note 7", "Mi Note 7 Pro", "Mi K20"]))
        mobileBrand.append(MobileBrand.init(brandName: "KTM", modelName: ["Mi Note 7", "Mi Note 7 Pro", "Mi K20"]))
        mobileBrand.append(MobileBrand.init(brandName: "NTMTM", modelName: ["Mi Note 7", "Mi Note 7 Pro", "Mi K20"]))
        mobileBrand.append(MobileBrand.init(brandName: "Huawei", modelName: ["Huawei Mate 20", "Huawei P30 Pro", "Huawei P10 Plus", "Huawei P20"]))
        
        
        self.collectionView.register(UINib(nibName: "TopSectionCVC", bundle: nil), forCellWithReuseIdentifier: "TopSectionCVC")


    }

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return mobileBrand.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mobileBrand[section].modelName?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = mobileBrand[indexPath.section].modelName?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return mobileBrand[section].brandName
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView === self.tableView {
            if let topSectionIndex = self.tableView.indexPathsForVisibleRows?.map({ $0.section }).sorted().first,
                let selectedCollectionIndex = self.collectionView.indexPathsForSelectedItems?.first?.row,
                selectedCollectionIndex != topSectionIndex {
                let indexPath = IndexPath(item: topSectionIndex, section: 0)
                self.collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
            }
        }
    }
    
}

extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return mobileBrand.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TopSectionCVC", for: indexPath) as! TopSectionCVC
        cell.lblTopSection?.text = mobileBrand[indexPath.row].brandName
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let selectedCategory = mobileBrand[indexPath.row].brandName
        let cellToReload = mobileBrand.filter{return $0.brandName == selectedCategory}[0]
        guard let indexPathsOfCellToRealod = mobileBrand.firstIndex(of: cellToReload) else { return }
        tableView.scrollToRow(at: IndexPath(row: 0, section: indexPathsOfCellToRealod), at: .top, animated: true)
    }
    
}

Currently, I have clicked on collectionView cell index that time auto scroll tableview with headersection look perfect. but i am not able to scroll till the last header section. So how can I do this? The critial line of code seems to be tableView.scrollToRow(at: IndexPath(row: 0, section: indexPathsOfCellToRealod), at: .top, animated: true)

0

There are 0 answers