Villva Tharisan
Villva Tharisan

Reputation: 92

Table View Scroll To Row

Here is my implementation.

Navigation from My Explore page:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let detailVM = NearMeSharedReviewVM(
        type: .trending,
        dataSource: viewModel.dataSource,
        currentPage: viewModel.currentPage,
        isFinalPage: viewModel.isFinalPage
    )
    
    let detailVC = NearMeSharedReviewVC(withViewModel: detailVM, selectedRow: indexPath.row)
    navigationController?.pushViewController(detailVC, animated: true)
}

My Details Page:

let selectedRow: Int

lazy var reviewTV: UITableView = {
    let tv = UITableView()
    
    tv.separatorStyle = .none
    tv.showsVerticalScrollIndicator = false
    tv.rowHeight = UITableView.automaticDimension
    tv.estimatedRowHeight = UITableView.automaticDimension
    tv.backgroundColor = .clear
    tv.contentInset = .zero
    
    tv.dataSource = self
    tv.delegate = self
    
    tv.mj_footer = refreshFooter
    
    tv.register(CombinedViewCell.self, forCellReuseIdentifier: CombinedViewCell.identifier)
    
    return tv
}()

And

override func viewDidLoad() {
     super.viewDidLoad()
        
     reviewTV.reloadData()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    
    if selectedRow > 0 {
        let indexPath = IndexPath(row: selectedRow, section: 0)
        reviewTV.performBatchUpdates(nil) { [weak self] _ in
            self?.reviewTV.scrollToRow(at: indexPath, at: .top, animated: false)
        }
        
        showGuiderView(from: vm.dataSource[selectedRow].reviewContent)
    }
}

private func showGuiderView(from review: [NearMeReviewContent]?) {
    if nearMeConfig.isGuideViewShowned {
        return
    }
    
    if let reviewContent = review, !reviewContent.isEmpty {
        let guideView = NearMeUserInteractionGuideVC()
        guideView.modalPresentationStyle = .overFullScreen
        present(guideView, animated: false)
    }
}

I'm facing difficulty to scroll to selected index position. My tableview cell height may be over the intended height for 1 content. Suspect due to rendering issues.

Hope could get help on this, let me know if needed further information or code snippets.

Upvotes: 0

Views: 69

Answers (0)

Related Questions