2017-07-26 4 views
0

Je souhaite lire l'audio en cliquant sur la cellule. Et changez l'image de bouton. Ils fonctionnent bien. Mais quand je suis défiler mon image de bouton 4 cellules change automatiquement. S'il vous plaît aider. Toute aide serait appréciée.Bouton Image Répété sur la cellule

@IBAction func playSong (_ sender : UIButton , event: UIEvent){ 


    let buttonPosition:CGPoint = sender.convert(.zero, to: table) 
    let indexPath = self.table.indexPathForRow(at: buttonPosition) 
    let cell = table.cellForRow(at: indexPath!) as? CustumCell 
    let a = URL(string : "http://www.abstractpath.com/files/audiosamples/sample.mp3") 


     if((audioPlayers) != nil){ 
      audioPlayers = nil 
     } 
     audioPlayers = AVPlayer(url: a!) 

     if sender.isSelected == false { 
      sender.isSelected = true 
      audioPlayers?.play() 
      cell?.play.setImage(UIImage(named : "homestop"), for: .normal) 
     }else{ 
      sender.isSelected = false 
      audioPlayers?.pause() 
      cell?.play.setImage(UIImage(named : "homeplay"), for: .normal) 
     } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let identifier = "CustumCell" 
     var cell: CustumCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustumCell 
     if cell == nil { 
      var nib : Array = Bundle.main.loadNibNamed("CustumCell",owner: self,options: nil)! 
      cell = nib[4] as? CustumCell 
     } 
     cell.reportView.isHidden = true 
     cell.play.tag = indexPath.row 
     cell.play.addTarget(self, action:#selector(playSong(_:event:)), for: .touchUpInside) 
     cell.homereport.tag = indexPath.row 
     cell.homereport.addTarget(self, action:#selector(showReportView(_:)), for: .touchUpInside) 
     return cell 
    } 

Répondre

0

Fondamentalement, chaque fois que vous faites défiler vers le bas/haut/gauche/droite et votre cellule marquée va hors limites alors chaque fois que vous faites défiler en arrière avec le cellForRowAt va être appelé une fois de plus.

Je vous sugest de créer le dictionnaire avec [UITableViewCell : Bool] et à l'intérieur:

if sender.isSelected == false { 
      sender.isSelected = true 
      audioPlayers?.play() 
      dic[cell] = true 
      cell?.play.setImage(UIImage(named : "homestop"), for: .normal) 
     }else{ 
      sender.isSelected = false 
      audioPlayers?.pause() 
      dic[cell] = false 
      cell?.play.setImage(UIImage(named : "homeplay"), for: .normal) 
     } 

plus tard à l'intérieur:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let identifier = "CustumCell" 
     var cell: CustumCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? CustumCell 
     if cell == nil { 
      var nib : Array = Bundle.main.loadNibNamed("CustumCell",owner: self,options: nil)! 
      cell = nib[4] as? CustumCell 
     } 
     cell.reportView.isHidden = true 
     cell.play.tag = indexPath.row 
     cell.play.addTarget(self, action:#selector(playSong(_:event:)), for: .touchUpInside) 
     cell.homereport.tag = indexPath.row 
     cell.homereport.addTarget(self, action:#selector(showReportView(_:)), for: .touchUpInside) 

     if dic[cell] { 
     // Set the image of the button or what ever you like to :) 
     } 
     return cell 
    }