我正在使用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)
}
}发布于 2017-06-21 21:19:32
在tableView(_: cellForRowAt:)中,您可以设置:
cell.name.textcell.duration.textcell.number.textcell.playChange.tag对于每个新单元格,因此当该单元格用于新内容时,这些值将更新。
但是您不会将playChange按钮的图像改回原来的位置,因此当您重用该单元时,它不会被更新。
当您轻触playChange按钮时,您将在tickClicked中更新该按钮的图像。因此,当在一个单元上调用tickClicked时,图像就会更新,以后重用该单元时不会将其改回原来的位置。
尝试在tableView(_: cellForRowAt:)中更改playChange按钮的值/图像/状态。
另外,看看UITableViewCell的方法prepareForReuse。此方法在单元格被重用之前调用,并使您有机会“重置”单元格。
但请注意,正如文档中所说的:
出于性能原因,应仅重置与内容无关的单元格属性,例如alpha、编辑和选择状态。在重用单元格时,tableView(_:cellForRowAt:)中的表视图委托应该始终重置所有内容。
希望这对你有帮助。
发布于 2017-06-21 21:18:19
如果可以,tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)会重用旧的表视图单元格。
您不需要在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)中设置playChange图像。因此,它重用了原来的表视图单元格中已经存在的图像。
https://stackoverflow.com/questions/44676255
复制相似问题