2017-06-21 60 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 :) 中的表視圖委託應該在重用單元時始終重置所有內容。

希望能幫到你。

相關問題