我正在尝试在 tableview 单元格中实现播放/暂停按钮。每个单元格都有一个按钮,每当用户单击它时,它应该更改按钮图像也需要调用所需的功能,滚动后也应该相同。
下面是我正在使用的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("productCell") as ? SepetCell
cell.onButtonTapped = {
//Do whatever you want to do when the button is tapped here
}
最佳答案
首先看到tableView Cell的每个按钮都会有一个与之关联的唯一标签,所以为了更新特定单元格的按钮,你必须在单元格中定义一个按钮的标签然后通过此标记添加到您的函数中,以对所选单元格的特定按钮执行操作。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell_identifier", for:
indexPath) as! CellClass
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(playpause), for: .touchUpInside)
}
@objc func playpause(btn : UIButton){
if btn.currentImage == UIImage(named: "Play") {
btn.setImage(UIImage(named : "Pause"), forState: .Normal)
}
else {
btn.setImage(UIImage(named : "Play"), forState: .Normal)
}
// perform your desired action of the button over here
}
关于ios - Swift 处理 TableviewCell 中的播放/暂停按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834606/