Skip to content

Instantly share code, notes, and snippets.

@AganCoder
Created November 1, 2019 13:03
Show Gist options
  • Save AganCoder/8a20524da735df9408a45eed18c2854a to your computer and use it in GitHub Desktop.
Save AganCoder/8a20524da735df9408a45eed18c2854a to your computer and use it in GitHub Desktop.
extension UITableView {
func indexPathForCellContainsView(_ view: UIView) -> IndexPath? {
guard let cell = view.ancestorOfType(UITableViewCell.self) as? UITableViewCell else { return nil }
return self.indexPath(for: cell)
}
}
extension UIView {
func ancestorOfType(_ t: AnyClass) -> UIView? {
if self.isKind(of: t) {
return self
} else {
return self.superview?.ancestorOfType(t) ?? nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment