I'm not sure if this question has already been asked but I couldn't find one asked for Swift after spending some time. A similar question could have been asked here, but it's for Objective-C and the question was vice-versa what I'm after.
I have a UIButton
inside a TableViewCell
which has some action once tapped on it, however, when UIButton
is clicked, only didSelectRowAt
tableView function is getting triggered. The UIButton
is in a separate TableViewCell
class. The TableViewCell
is expandable, so when each row is tapped it expands/collapses. I'm sure there must be a way of controlling this with UITapGestureRecognizer
, but I wouldn't know how to manipulate coordinates as I'm relatively new to Swift.
SomeTableViewCell.swift
class SomeTableViewCell: UITableViewCell {
@IBAction func activateButtonTapped(_ sender: Any) {
activateTapButton?(self)
}
var activateTapButton: ((UITableViewCell) -> Void)?
}
ViewController.swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var someTableView: UITableView!
var selectedIndex: Int = -1
var someNumber = 123456789
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = servicesTableView.dequeueReusableCell(withIdentifier: “cell", for: indexPath) as! SomeTableViewCell
cell.activateButton.isUserInteractionEnabled = true
cell.activateTapButton = {(Void) in
if let url = URL(string: "tel://\(someNumber)"), UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(selectedIndex == indexPath.row) {
return 280
} else {
return 60
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//indexPath handling once cell clicked
// Expanding cell feature
if (selectedIndex == indexPath.row) {
selectedIndex = -1
} else {
selectedIndex = indexPath.row
}
self. someTableView.beginUpdates()
self. someTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
self. someTableView.endUpdates()
}