Skip to content

Instantly share code, notes, and snippets.

@tosinonikute
Created July 16, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tosinonikute/a2b05217dcd54314b63a84dd529ffeff to your computer and use it in GitHub Desktop.
Save tosinonikute/a2b05217dcd54314b63a84dd529ffeff to your computer and use it in GitHub Desktop.
import UIKit
protocol TableViewCellDelegate {
func buttonTapped(_ cell: SampleTableViewCell)
}
class SampleTableViewCell: UITableViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var button: UIButton!
var delegate: TableViewCellDelegate? //create delegate
//create action of the cell button
@IBAction func didTap(_ sender: UIButton){
delegate?.buttonTapped(self)
}
}
class TableViewController: UITableViewController, TableViewCellDelegate {
var tableArray = ["New York", "Chicago", "North Island"]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.reloadData()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return self.tableArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SampleCell", for: indexPath) as! SampleTableViewCell
cell.delegate = self
// Configure the cell...
cell.selectionStyle = UITableViewCell.SelectionStyle.none
let names = self.tableArray[indexPath.row]
cell.name.text = names
return cell
}
@objc fileprivate func alertMethod() {
let alert = UIAlertController(title: "title 1", message: "Button Clicked!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
func buttonTapped(_ cell: SampleTableViewCell) {
// Do the action you want to do here on cell button tap
DispatchQueue.main.async {
let alert = UIAlertController(title: "title 2", message: "Button Clicked!", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
self.present(alert, animated: true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment