每个10个单元格都重复TableView
我正在使用UITableView
内的AVPlayer
。当我更改图像按钮时,每个10格重复。为什么每10个单元重复一次以及如何修复?注意超类。每个10个单元格都重复TableView
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(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
重新使用旧的tableview细胞。
您不会在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
中设置playChange
图像。因此,它重新使用原始tableview单元格中已存在的图像。
在你tableView(_: cellForRowAt:)
您设置:
cell.name.text
cell.duration.text
cell.number.text
cell.playChange.tag
为每一个新的细胞,所以这些值已经更新单元格时被重新使用新的内容。
但是,您不会将您的playChange
按钮的图像更改回来,所以在您重新使用该单元格时它不会更新。
当您点击您的playChange
按钮时,您更新tickClicked
中按钮的图像。因此,在您的某个单元格上调用tickClicked
时,图像会更新,并且稍后重新使用该单元格时不会再将其更改。
尝试更改您的playChange
按钮的值/图像/状态tableView(_: cellForRowAt:)
。
另外,看看UITableViewCell
的方法prepareForReuse
。这个方法在单元重用之前被调用,并给你一个“重置”单元的机会。
公告不过,因为它在文档中说:
出于性能考虑,你应该只重置不相关的内容,例如,阿尔法,编辑和选择状态的细胞的属性。 tableView(_:cellForRowAt :) 中的表视图委托应该在重用单元时始终重置所有内容。
希望能帮到你。
如果条件为 –
,您是否尝试添加'cell.tag = indexPath.row'您的表代码看起来没问题。你确定'modalsF'不是你麻烦的根源吗? –
@LarryOBrien ModalsF存储数据 – Vasya2014