【问题标题】:Button Image Repeated On cell按钮图像在单元格上重复
【发布时间】:2024-01-24 03:15:01
【问题描述】:

我想在单击单元格时播放音频。并更改按钮图像。它们工作正常。但是当我滚动我的 4 单元格按钮图像时会自动更改。请帮忙。任何帮助将不胜感激。

   @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
    }

【问题讨论】:

    标签: audio swift3 avaudioplayer


    【解决方案1】:

    基本上,每当您向下/上/左/右滚动并且标记的单元格超出范围时,然后每当您滚动返回时,cellForRowAt 将再次被调用。

    我建议你用[UITableViewCell : Bool] 和内部创建字典:

    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)
            }
    

    稍后在里面:

    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
        }
    

    【讨论】: