custom cell:
import UIKit
typealias ActionCallback = () -> Void
class ActionLinkCell: UITableViewCell {
var cb: ActionCallback? = nil
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifier)
self.accessoryType = .disclosureIndicator
self.textLabel?.textColor = self.tintColor
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ActionLinkCell.handleIsTap(_:)))
self.addGestureRecognizer(tapGesture)
}
@objc func handleIsTap(_ sender: UIGestureRecognizer) {
debugPrint("\(#function)")
if let callback = self.cb {
callback()
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func config(title: String, callback: ActionCallback? = nil) {
self.textLabel?.text = title
self.setNeedsDisplay()
self.cb = callback
}
}
calling code:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: bluetoothActionLinkCellId, for: indexPath) as! ActionLinkCell
let message = NSLocalizedString("Pair device using Bluetooth", comment: "")
cell.config(title: message) {
//MY CODE GOES HERE
}
return cell
}