i have a table view with a custom cell that has a button .. if i clicked on the button an action should be done ..
the problem is that the button is not cliked and the whole cell is clicked and the action i want never happen!
how to solve that? i want to click on the button not the cell and do specific action when clicked..
here what i'm doing but didnt work..
the custom cell:
class CustomViewCell8: UITableViewCell {
var onButtonTapped : (() -> Void)? = nil
@IBOutlet weak var servicebtn: UIButton!
@IBAction func Clicked(_ sender: UIButton) {
if let onButtonTapped = self.onButtonTapped{
onButtonTapped()
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
and in the view controller:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "CustomCell8") as! CustomViewCell8
cell.servicebtn.setBackgroundImage(UIImage(named: serviceimage[indexPath.row]) as UIImage?, for: UIControlState.normal)
cell.onButtonTapped = {
if self.number == 1 {
if indexPath.row == 0{
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: " ") as UIViewController
self.present(vc, animated: true, completion: nil)
let userDefaults = UserDefaults.standard
let decoded = userDefaults.object(forKey: "services") as! Data
let decodedService = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Service]
for service in decodedService {
if service.name == " "{
GetShiftsAndPrices(id: service.id)
}
}
}else if indexPath.row == 1{
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: " ") as UIViewController
self.present(vc, animated: true, completion: nil)
let userDefaults = UserDefaults.standard
let decoded = userDefaults.object(forKey: "services") as! Data
let decodedService = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Service]
for service in decodedService {
if service.name == " "{
GetShiftsAndPrices(id: service.id)
}
}
}else if indexPath.row == 2{
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: " ") as UIViewController
self.present(vc, animated: true, completion: nil)
let userDefaults = UserDefaults.standard
let decoded = userDefaults.object(forKey: "services") as! Data
let decodedService = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! [Service]
for service in decodedService {
if service.name == " "{
GetShiftsAndPrices(id: service.id)
}
}
}
......
return cell
}
this was working with me before .. but now not working! i don't know what is wrong .. am i doing something wrong?