简体   繁体   中英

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.

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")
    }

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

Why don't you use the UITableViewDelegate ?

@available(iOS 2.0, *)
optional func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath)

To clarify that, yet it is possible. You can have custom UI elements act as accessory button.

Here is an example – assuming you're using Interface Builder. Add a UISwitch to the custom cell. Now right-click drag-n-drop from the UITableViewCell to the UISwitch . Select accessoryView as the Outlet connection. Done.

Every time you press the switch button the delegate method will be fired. This should also work for other action elements, eg, UIButton s. And that is exactly the use case OP asked for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM