Ios 每10个单元格重复一次TableView
我正在UITableView中使用AVPlayer。当我更改图像按钮时,每个10个单元格重复。为什么每10个单元都会重复,如何修复?注意超类Ios 每10个单元格重复一次TableView,ios,swift,uitableview,tableview,avplayer,Ios,Swift,Uitableview,Tableview,Avplayer,我正在UITableView中使用AVPlayer。当我更改图像按钮时,每个10个单元格重复。为什么每10个单元都会重复,如何修复?注意超类 var boolValue = false class TableViewControllerAudioList: UIView,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var tableView: UITableView! var avplayer:AVPlayer
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)
}
}
tableView_utableview:UITableView,cellForRowAt indexath:indexath在可能的情况下重新使用旧的tableView单元格
在tableView\uTableView:UITableView、CellForRow和indexPath:indexPath中不设置播放更改图像。因此,它重新使用了原始tableview单元格中已经存在的图像 在您的tableView中:cellForRowAt:您设置了:
cell.name.text
cell.duration.text
cell.number.text
cell.playChange.tag
对于每个新单元格,当单元格与新内容一起重用时,这些值将更新
但是,您不会将playChange按钮的图像更改回原来的位置,因此当您重新使用单元格时,它不会被更新
当您点击playChange按钮时,您将更新单击按钮中按钮的图像。因此,当在您的一个单元格上调用tickClicked时,图像将被更新,并且您不会在以后重用该单元格时将其更改回原来的状态
尝试更改tableView中播放更改按钮的值/图像/状态。\uu:cellForRowAt:
另外,请看一下UITableViewCell的方法。此方法在重新使用单元格之前调用,并使您有机会重置单元格
但请注意,正如文档中所述:
出于性能原因,您应该只重置与内容无关的单元格属性,例如alpha、编辑和选择状态。中的表视图的委托
tableView_u3;:cellForRowAt:
重复使用单元格时,应始终重置所有内容
希望这对您有所帮助。如果表代码看起来正常,是否尝试添加cell.tag=indexPath.row条件。你确定modalsF不是你的问题的根源吗?@LarryOBrien modalsF存储数据i设置播放更改图像。单元格在滚动时不保存图像,Gif有问题在显示的代码中,在tableView中不使用cell.playChange.setImage\uTableView:UITableView,CellForRow在indexPath:indexPath中。这意味着重复使用的单元格中仍然有旧的图像。随着代码的更改,您会遇到不同的问题。在tableView_uTableView:UITableView、cellForRowAt indexPath:indexPath中,需要将单元格的图像设置为适用于该单元格的图像。也就是说,您需要跟踪正在播放的歌曲。然后将单元格图像设置为play.png或pause.png,具体取决于歌曲是否正在播放。