如何使用swift在表格视图单元格中只能点击一个按钮

问题描述

我有一个表格视图,每个单元格都有按钮。每个按钮为每个单元格播放不同的歌曲并将图像更改为“播放”或“暂停”。但是我有一个问题,当我点击两个或三个按钮时,它们会将照片更改为“暂停”。它应该只更改其中一张照片。检查照片:Buttons in cell

视图控制器中有我的代码

extension BeatPackViewController: UITableViewDataSource,UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 12
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: CustomLoopsCell = beatTableView.dequeueReusableCell(withIdentifier: "firstLoopCell",for: indexPath) as! CustomLoopsCell
        gettingSongName()
        
        cell.loopNameLabel.text = data[indexPath.row].loop_name
        cell.producerLabel.text = data[indexPath.row].producer
        cell.instrumentLabel.text = data[indexPath.row].Instrument
        cell.delegate = self
        cell.selectionStyle = .none
        
        cell.tag = indexPath.row
        if let playingCell = currentPlayingIndex {
            if playingCell == indexPath.row {
                cell.playButtonOutlet.setimage(UIImage(named: "Pause.png"),for:
                                                .normal)
            }
        } else {
            cell.playButtonOutlet.setimage(UIImage(named: "playBtn.png"),for:
                                            .normal)
        }
    //        cell.instrumentLabel.text = data[indexPath.row].loops[indexPath.row].Instrument
    //        cell.producerLabel.text = data[indexPath.row].loops[indexPath.row].producer
    return cell
}
    
    func btnUseTap(cell: CustomLoopsCell) {
        
        let indexPath = self.beatTableView.indexPath(for: cell)
        if currentPlayingIndex == cell.tag {
                audioPlayer.pause()
                currentPlayingIndex = nil
            beatTableView.reloadData()
             } else { //IF PAUSE BUTTON
                playLoop(song_name: songs[cell.tag])
                currentPlayingIndex = cell.tag
                beatTableView.reloadData()
             }
            beatTableView.reloadData()
//        playSong(index: indexPath!.row)
        print("Done")
    }

解决方法

1)首先创建按钮的空数组,例如:

let allButtons: [UIButton] = []

2)创建每个单元格时,将该单元格的按钮添加到数组中,示例代码:

allButtons.append(yourButton)

3)创建将所有按钮静音并为其分配暂停图像的功能,例如:

    func muteAllButtons() {
    for button in allButtons {
        button.muteThisButton()
        button.setImageToPlay()
    }
}
  1. 创建处理所有按钮静音,然后从选定按钮播放音乐的函数,例如:

     func userSelectedButton(at yourSelectedCellIndex: Int) {
     muteAllButtons()
     let currentPlayingButton = allButtons[yourSelectedCellIndex]
     currentPlayingButton.playMusic()
     currentPlayingButton.setImageToPause()
    

    }

  2. 当用户点击选中的单元格时,调用 userSelected 函数。例如:

    userSelectedButton(at: yourCellIndex)

,

看起来您的 if 有问题:

if let playingCell = currentPlayingIndex {
    if playingCell == indexPath.row {
        cell.playButtonOutlet.setImage(UIImage(named: "Pause.png"),for:
                                        .normal)
    }
} else {
    cell.playButtonOutlet.setImage(UIImage(named: "playBtn.png"),for:
                                    .normal)
}

currentPlayingIndex != nilplayingCell != indexPath.row时,它不更新图像,所以它从出列的单元格中获取随机图像。改为:

if let playingCell = currentPlayingIndex,playingCell == indexPath.row {
    cell.playButtonOutlet.setImage(UIImage(named: "Pause.png"),for:
                                    .normal)
} else {
    cell.playButtonOutlet.setImage(UIImage(named: "playBtn.png"),for:
                                    .normal)
}

这里还有多余的 reloadData

if currentPlayingIndex == cell.tag {
    audioPlayer.pause()
    currentPlayingIndex = nil
 beatTableView.reloadData()
 } else { //IF PAUSE BUTTON
    playLoop(song_name: songs[cell.tag])
    currentPlayingIndex = cell.tag
    beatTableView.reloadData()
 }
beatTableView.reloadData()

只需从 if/else 中删除两个,然后在 if/else 之后留下一个。