Skip to content

Instantly share code, notes, and snippets.

@rsaenzi
Last active March 5, 2021 03:13
Show Gist options
  • Save rsaenzi/13dc00b5cb5a09efa84eaff3ff4682af to your computer and use it in GitHub Desktop.
Save rsaenzi/13dc00b5cb5a09efa84eaff3ff4682af to your computer and use it in GitHub Desktop.
Swift code to detect touches for buttons placed inside a UITableViewCell or UICollectionViewCell
//
// OnTapButtonInsideCell.swift
//
// Created by Rigoberto Sáenz Imbacuán (https://www.linkedin.com/in/rsaenzi/)
// Copyright © 2021. All rights reserved.
//
private func onTapButtonInsideCell(button: UIButton, event: UIEvent) {
guard let touches = event.touches(for: button), !touches.isEmpty else {
return
}
let touch = touches[touches.startIndex]
let point = touch.location(in: tableView)
guard let indexPath = tableView.indexPathForItem(at: point) else {
return
}
print("onTapButtonInsideCell() indexPath.row=\(indexPath.row)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment