ios - Swift 处理 TableviewCell 中的播放/暂停按钮

标签 ios swift uitableview

我正在尝试在 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/

相关文章:

ios - 在 Swift 中为 UIViewController 自定义初始化,在 Storyboard 中设置界面

ios - 应用程序内购买不起作用 Swift

swift - 在异步任务后插入 UIViewtable

ios - 从 NSUserdefaults 中删除 NSDictionary

c# - 在 Unity 中创建 iOS 插件

objective-c - 如何调用encodeWithCoder

objective-c - 在 xcode 中使用线程调用函数

ios - 重新加载数据时 TableView 闪烁

ios - Swift/iOS 等同于 Android 中的微调器?

ios - 导航 Controller 未推送另一个 View