George Avgoustis George Avgoustis - 8 months ago 78
iOS Question

Access parent component for ScrollView delegate

I've got a UICollectionView and a UITableView on the same controller. For reasons, I also need to implement some scrollView delegate methods. How can I distinguish which component is triggering the delegate ( e.g is it the table or the UICollectionView? )

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {

//need to distinguish which component
//triggered the delegate here

snapToNearestCell(collectionView)
}


An idea is to add a tag to the UIScrollView for each component ( even though the scroll view isn't so straightforward to access and you need to iterate over the components views)

Any other ideas, best practices?

Answer Source

The table view and the collection view are the scroll view that's passed into that method. Set tags to the table view and the collection view and you are able to reason about who called that delegate method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download