sphynx
sphynx

Reputation: 413

addTarget for button in tableView

I'm trying to add the download button for some items in my tableView. I've created the custom cell class and added the label and the button outlets, everything is working in displaying the info and even the buttons are showing where it should be.

I'm trying to add the target, but it does nothing. I need to pass the row index to the buttonClicked function or should I create this function in the custom cell class and then do the action some how? I would like to know the best practise of this.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "PlaylistCell", for: indexPath) as! PlaylistTableViewCell

        let playlist = self.playlists?[indexPath.row]

        cell.titleLabel.text = playlist?.getTitle()


        if (playlist?.isOfflineAvailable())! {
            cell.downloadButton.isHidden = false
        } else {
            cell.downloadButton.isHidden = true
            cell.downloadButton.tag = indexPath.row
            cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
        }

        return cell
    }

    func buttonClicked(sender: UIButton) {
        let buttonRow = sender.tag
        print(buttonRow)
    }

I've also tried removing the (sender:) from #selector, but it does not change a functionality.

Upvotes: 1

Views: 7024

Answers (1)

alexburtnik
alexburtnik

Reputation: 7741

In order to handle button callback in your view controller, you have two choices:

Target-action:

Add target-action in cellForRow method just as you did. Your code is probably not working because you are hiding the button when it should be visible, aren't you?

I guess you need to replace this

if (playlist?.isOfflineAvailable())! {
    cell.downloadButton.isHidden = false
} else {
    cell.downloadButton.isHidden = true
    cell.downloadButton.tag = indexPath.row
    cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
}

With this:

cell.downloadButton.isHidden = playlist?.isOfflineAvailable()
cell.downloadButton.tag = indexPath.row
cell.downloadButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)

You should update tag every time because cell are reused in tableView and if don't do it every time when cellForRow is called, you may easilly get a case when a callback is called but it's tag belongs to indexPath from the previous cell usage. Also I've changed isHidden logics to the opposite. I guess you should hide the button when isOfflineAvailable returns true, right?

Delegate pattern

It is described a million of times here on SO and on many other sites as well. Basically you define a cell protocol, implement it in your controller and send callbacks from cell to it's delegate whenever a button is pressed. You can find more details in my answer for a similar question.

Upvotes: 6

Related Questions