2017-06-21 56 views
0

我正在使用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) 
    }  
} 
+0

如果条件为 –

+0

,您是否尝试添加'cell.tag = indexPath.row'您的表代码看起来没问题。你确定'modalsF'不是你麻烦的根源吗? –

+0

@LarryOBrien ModalsF存储数据 – Vasya2014

回答

0

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)重新使用旧的tableview细胞。

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

+0

我设置了playChange图像。在滚动时单元格不保存图像,有问题的Gif – Vasya2014

+0

在显示的代码中,不要在'tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)'中使用'cell.playChange.setImage'。这意味着重新使用的单元格中仍然有旧图像。 – fishinear

+0

忘记再次更改代码检查 – Vasya2014

1

在你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 :) 中的表视图委托应该在重用单元时始终重置所有内容。

希望能帮到你。

相关问题