Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying which button pressed in a custom listView cell in Swift

I've created a custom cell for a listView in Swift. It has two buttons on it - one a 'pause' button and one a 'stop' button. The idea is that each line item represents a download so the user can stop and start each independently.

However, I need to create an @IBAction for each of the buttons. I've created these in the main ViewController and sure enough, when they are hooked up, they trigger appropriate.

The bit I'm stuck on is identifying an identifier of which row's button has been pressed. I'm assuming something relating to the cellForRowAtIndexPath would work.

I've found the following code (which I found from a similar question concerning text fields):

@IBAction func startOrPauseDownloadSingleFile(sender: UIButton!) {
    let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
    let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)
}

However I keep getting the error 'Cannot invoke 'convertPoint' with an argument list of type (@lvalue CGPoint, toView: $T6)'.

Can anyone help?

Thanks,

like image 576
Oliver Spencer Avatar asked Sep 01 '14 19:09

Oliver Spencer


1 Answers

I took your code and embedded it into a simple project.

In Interface Builder, I created a UITableViewController scene and set its class to "ViewController". I added to it a UITableViewCell, set its identifier to "Cell", set its style to "Custom" and its class to "CustomCell". I then added a UIButton in the cell's contentView and set non ambiguous auto layout constraint to it.

In Project Navigator, I created a new file called "ViewController" and added the following code in it:

import UIKit

class CustomCell: UITableViewCell {

    @IBOutlet weak var button: UIButton!

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

}

class ViewController: UITableViewController {

    func buttonPressed(sender: AnyObject) {
        let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
        let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)
        println(cellIndexPath)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomCell
        cell.selectionStyle = .None

        cell.button.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)

        return cell
    }

}

I finally linked the button's IBOutlet to the UIButton in Interface Builder, ran the project and was able to log respective cell's indexPaths from each button touch.

like image 182
Imanou Petit Avatar answered Oct 23 '22 06:10

Imanou Petit