Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the action for custom accessory view button - swift

This seems to have been asked a few times in swift and objc, but I can't see a correct answer for swift so hopefully someone can help me this time. I have created a custom accessory view button, but need the correct button action: as "accessoryButtonTapped" is an unrecognised selector.

What is the selector needed to call the tableViewCellDelegate method willSelectRowAtIndexPath?

The code I have is:

let cellAudioButton = UIButton(type: .Custom)
cellAudioButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
cellAudioButton.addTarget(self, action: "accessoryButtonTapped", forControlEvents: .TouchUpInside) //INCORRECT ACTION:
cellAudioButton.setImage(UIImage(named: "blueSpeaker.png"), forState: .Normal)
cellAudioButton.contentMode = .ScaleAspectFit
cell.accessoryView = cellAudioButton as UIView

Thanks in advance to anyone who can help.

like image 208
richc Avatar asked Jan 13 '16 22:01

richc


2 Answers

Why do you need to call willSelectRowAtIndexPath? You have done everything right and to solve your unrecognized selector error just make a function that will be called when you tap on the cell.accessoryView. In your case:

func accessoryButtonTapped(){
    print("Tapped")
}

Update
If you want to get the indexPath you could just

Add a tag to your cellAudioButton:

cellAudioButton.tag = indexPath.row

In your addTarget add a : to pass a parameter

And in your function

func accessoryButtonTapped(sender : AnyObject){
        print(sender.tag)
        print("Tapped")
    }

So the whole code:

let cellAudioButton = UIButton(type: .Custom)
        cellAudioButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        cellAudioButton.addTarget(self, action: "accessoryButtonTapped:", forControlEvents: .TouchUpInside)
        cellAudioButton.setImage(UIImage(named: "blueSpeaker.png"), forState: .Normal)
        cellAudioButton.contentMode = .ScaleAspectFit
        cellAudioButton.tag = indexPath.row
        cell.accessoryView = cellAudioButton as UIView

func accessoryButtonTapped(sender : AnyObject){
        print(sender.tag)
        print("Tapped")
    }
like image 80
Rashwan L Avatar answered Nov 10 '22 05:11

Rashwan L


Swift 3.1 Updated Solution of Rashwan

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
       //add button as accessory view
        let cellAudioButton = UIButton(type: .custom)
        cellAudioButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        cellAudioButton.addTarget(self, action: #selector(ViewController.accessoryButtonTapped(sender:)), for: .touchUpInside)
        cellAudioButton.setImage(UIImage(named: "closeRed"), for: .normal)
        cellAudioButton.contentMode = .scaleAspectFit
        cellAudioButton.tag = indexPath.row
        cell.accessoryView = cellAudioButton as UIView

        return cell
    }
func accessoryButtonTapped(sender : UIButton){
        print(sender.tag)
        print("Tapped")
    }

Note:

Here ViewController is the name of Class like LoginViewController

like image 6
Sourabh Sharma Avatar answered Nov 10 '22 04:11

Sourabh Sharma