Reputation: 513
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?
Upvotes: 0
Views: 57
Reputation: 154711
You indicated that even a print("here")
in your @IBAction
for the button isn't printing. You also indicated that your @IBAction
was wired up correctly. That means there is a problem with your prototype cell. If the view containing the button is smaller than the button, then you won't be able to click it. Check the layout of your prototype cell.
Original Answer
That looks like it should work.
Three comments:
You should add [unowned self]
to your closure declaration to avoid a strong reference loop which will prevent the UITableView
from being freed. Since the cell will not outlive its containing UITableView
, there is no need for it to hold a strong reference to self
.
cell.onButtonTapped = { [unowned self]
if self.number == 1 {
...
You can use optional chaining to replace:
if let onButtonTapped = self.onButtonTapped{
onButtonTapped()
}
with simply:
self.onButtonTapped?()
You code is very repetitive. If the only difference between the rows is the identifier for the next ViewController, then you could do:
let nextvcid = ["foo", "bar", "baz"][indexPath.row]
cell.onButtonTapped = { [unowned self]
if self.number == 1 {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: nextvcid)
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)
}
}
Upvotes: 1