【发布时间】: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