0
我试图运行一个独特的功能时,每个部分的每一行中单击按钮。 问题例如,如果我在3部分中有3行,并且配置第一行以在按下按钮时运行函数,则所有3部分的第一行运行相同的函数。我试图实现为不同部分中的所有行运行独特的功能。Swift 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
}
这是Button
的IBAction
:
@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;
}
}