【问题标题】:each 10 cell is repeated TableView每 10 个单元格重复 TableView
【发布时间】:2017-06-21 12:33:22
【问题描述】:

我正在使用UITableView 中的AVPlayer。当我更改图像按钮时,每 10 个单元格重复。为什么每 10 个单元格重复以及如何修复它?注意超类。

  var boolValue = false

 class TableViewControllerAudioList: UIView,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var avplayer:AVPlayer!




override func setNeedsDisplay() {
    super.setNeedsDisplay()


}



 func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return modalsF.count
}


 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellAudioList


    cell.name.text = modalsF[indexPath.row].AudioName
    cell.duration.text = "00:00"
    cell.number.text = "\(indexPath.row + 1)"

    cell.playChange.setImage(UIImage(named:"Play.png"), for: UIControlState.normal)


    cell.playChange.tag = indexPath.row
    cell.playChange.addTarget(self, action: #selector(tickClicked), for: .touchUpInside)

    cell.tapAction = { (cell) in







   // self.player = self.setupAudioPlayerWithFile(file: "https:----.com" + modalsF[indexPath.row].UrlName! as NSString, type: "mp3")

    self.avplayer = self.pl(file: "https:---.com" + modalsF[indexPath.row].UrlName! as NSString)


       // self.play(tableView.indexPath(for: cell)!.row)

        }


     return cell
}



func tickClicked(_ sender: UIButton!) {
    print(sender.tag)
    let cellTop = tableView.cellForRow(at: NSIndexPath(row: sender.tag, section: 0) as IndexPath) as!TableViewCellAudioList

    if boolValue == false{
        cellTop.playChange.setImage(UIImage(named:"Pause.png"), for: UIControlState.normal)
        boolValue = true
    } else {
        cellTop.playChange.setImage(UIImage(named:"Play.png"), for: UIControlState.normal)
        boolValue = false
    }
}




func pl(file:NSString) -> AVPlayer? {

    let url = URL(string: file as String)
    let avPlayer: AVPlayer?
    let playerItem = AVPlayerItem(url: url!)
    avPlayer = AVPlayer(playerItem:playerItem)



    if avPlayer?.rate == 0 {
        avPlayer?.play()
        avPlayer?.rate = 1.0

    } else if avPlayer?.rate == 1{

        avPlayer?.pause()

    }

    return avPlayer

}

TableViewCell:

class TableViewCellAudioList: UITableViewCell {

@IBOutlet weak var number: UILabel!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var duration: UILabel!
@IBOutlet weak var playChange: UIButton!



var tapAction: ((UITableViewCell) -> Void)?
@IBAction func playAudio(_ sender: Any) {
    tapAction?(self) 
  }    
}

【问题讨论】:

  • 你有没有尝试在if条件下添加cell.tag = indexPath.row
  • 您的表格代码看起来不错。你确定modalsF不是你的麻烦吗?
  • @LarryOBrien ModalsF 存储数据

标签: ios swift uitableview tableview avplayer


【解决方案1】:

在您的tableView(_: cellForRowAt:) 中设置:

  • cell.name.text
  • cell.duration.text
  • cell.number.text
  • cell.playChange.tag

对于每个新单元格,因此当单元格与新内容一起使用时,这些值会更新。

但是您不会将playChange 按钮的图像改回来,因此当您重复使用单元格时它不会更新。

当您点击playChange 按钮时,您会更新tickClicked 中的按钮图像。因此,当在您的一个单元格上调用 tickClicked 时,图像会更新,并且您以后重用该单元格时不会将其更改回来。

尝试在tableView(_: cellForRowAt:) 中更改playChange 按钮的值/图像/状态。

另外,看看UITableViewCell 的方法prepareForReuse。在重用单元格之前调用此方法,让您有机会“重置”单元格。

请注意,正如文档中所说:

出于性能原因,您应该只重置与内容无关的单元格属性,例如 alpha、编辑和选择状态。表视图的委托 tableView(_:cellForRowAt:) 重用单元格时应始终重置所有内容。

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 尽可能重复使用旧的 tableview 单元格。

    您没有在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 中设置playChange 图像。因此,它重新使用了原始 tableview 单元格中已经存在的图像。

    【讨论】:

    • 我设置了 playChange 图像。滚动时单元格不保存图像,Gif 有问题
    • 在您显示的代码中,您没有在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 中使用cell.playChange.setImage。这意味着重复使用的单元格中仍然有旧图像。
    • 使用更改后的代码,您会遇到不同的问题。在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 中,您需要将单元格的图像设置为适用于该单元格的图像。也就是说,您需要跟踪正在播放的歌曲。然后将单元格的图像设置为“play.png”或“pause.png”,具体取决于该歌曲是否正在播放。
    猜你喜欢
    • 2019-11-10
    • 2021-12-09
    • 2020-08-01
    • 1970-01-01
    • 2019-05-09
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    相关资源
    最近更新 更多