Geniouse
Geniouse

Reputation: 189

Swift 3: How to use action button in multiple sections of a table view?

I'm trying to run a unique function when a button is clicked in each row of each section. The problem for example is, if i have 3 rows in 3 sections and i configure the first row to run a function when the button is pressed, the first row for all 3 sections runs the same function. I'm trying to achieve running unique functions for all rows in different sections.

This is my tableView code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: languageID, for: indexPath) as! LanguageTableViewCell

    switch indexPath.row {
    case 0:
        cell.phraseLBL.text = "description 1"
        cell.playBtn.tag = indexPath.row
        cell.playBtn.addTarget(self, action: #selector(PhraseVC.pressPlay), for: .touchUpInside)

    case 1:
        cell.phraseLBL.text = "description 2"
        cell.playBtn.tag = indexPath.row
        cell.playBtn.addTarget(self, action: #selector(PhraseVC.pressPlay), for: .touchUpInside)

    case 2:
        cell.phraseLBL.text = "description 3"
        cell.playBtn.tag = indexPath.row
        cell.playBtn.addTarget(self, action: #selector(PhraseVC.pressPlay), for: .touchUpInside)
    default:
        break
    }

    return cell
}

And this is the IBAction of Button:

@IBAction func pressPlay(sender: UIButton){

        switch sender.tag {
        case 0:
            print("button 1 pressed")

        case 1:
            print("button 2 pressed")

        case 2:
            print("button 3 pressed")

        case 3:
            print("button 4 pressed")

        case 4:
            print("button 5 pressed")
        default:
            break;
        }
    }

Upvotes: 0

Views: 3114

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you can do like

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: languageID, for: indexPath) as! LanguageTableViewCell
    
    switch indexPath.row {
    case 0:
        cell.phraseLBL.text = "description 1"
        
        
    case 1:
        cell.phraseLBL.text = "description 2"
        
    case 2:
        cell.phraseLBL.text = "description 3"
       
    default:
        break
    }
    
    cell.playBtn.tag = indexPath.row
    cell.playBtn.addTarget(self, action: #selector(PhraseVC.pressPlay(_:)), for: .touchUpInside)
  
    
    return cell
}

and call the method as

@IBAction func pressPlay(_ sender: UIButton){
    
    let touchPoint = sender.convert(CGPoint.zero, to:maintable)
    // maintable --> replace your tableview name
    
    let clickedButtonIndexPath = mainTable(forRowAtPoint: touchPoint)
    
   
    
}

For Latest Swift Versions :

 @IBAction func pressPlay(_ sender: UIButton){
    let touchPoint = sender.convert(CGPoint.zero, to:self.tableView)
    // tableView --> replace your tableview name
    if let clickedButtonIndexPath = self.tableView.indexPathForRow(at: touchPoint) {
        print("Section : \(clickedButtonIndexPath.section) , Row:  \(clickedButtonIndexPath.row)")
    }
}

Upvotes: 4

Max Pevsner
Max Pevsner

Reputation: 4104

You can set to each cell an Index Path and not tag. IndexPath consists of section and row. By checking to which section and which row the button belongs you can uniquely identify the correct function.

Upvotes: 0

Related Questions