簡體   English   中英

Swift 3:如何在表格視圖的多個部分中使用操作按鈕?

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

當在每個部分的每一行中單擊一個按鈕時,我試圖運行一個獨特的功能。 例如,問題是,如果我在 3 個部分中有 3 行,並且我將第一行配置為在按下按鈕時運行一個功能,則所有 3 個部分的第一行運行相同的功能。 我正在嘗試為不同部分的所有行運行獨特的功能。

這是我的tableView代碼:

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
}

這是ButtonIBAction

@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;
        }
    }

你可以喜歡

 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
}

並將該方法稱為

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

對於最新的 Swift 版本:

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

您可以為每個單元格設置索引路徑而不是標簽。 IndexPath由部分和行組成。 通過檢查按鈕屬於哪個部分和哪一行,您可以唯一地識別正確的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM