如果在 UITableView 中点击单元格并在离开后隐藏,则显示按钮

Show button if cell is tapped and hide after leave in UITableView

如果有人点击单元格或选择单元格,则按钮将显示,否则将被隐藏。只有正在播放当前歌曲的那个单元格才会显示按钮(它用于播放或停止)但问题是如果没有隐藏它,单元格的每一行都会显示按钮我也不想隐藏按钮如果我再次点击正在播放的歌曲单元格。

class AudioListViewController: UIViewController {
    var selectedRowIndex = -1
    var songList: [Audio] = [
        Audio(id: 0, trackName: "Song 1", trackTitle: "ABbcd:", album: "Loreim loreim",
              uploadedBy: "A men", genre: "Romantic", lyricsLang: "English", artist: "God",
              duration: 2.00),
        Audio(id: 1, trackName: "Song 2", trackTitle: "ABbcd:", album: "Loreim loreim",
              uploadedBy: "A men", genre: "Romantic", lyricsLang: "English", artist: "God",
              duration: 2.00),
        Audio(id: 2, trackName: "Song 3", trackTitle: "ABbcd:", album: "Loreim loreim",
              uploadedBy: "A men", genre: "Romantic", lyricsLang: "English", artist: "God",
              duration: 2.00)
    ]
    @IBOutlet weak var songListTableView: UITableView!
    var isTapped: Bool = false
    @IBOutlet weak var close: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        songListTableView.dataSource = self
        songListTableView.delegate = self
        
        songListTableView.register(UINib(nibName: "SongsListTableViewCell", bundle: nil),
                                   forCellReuseIdentifier: "SongListCell")
    }
}

extension AudioListViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return songList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SongListCell",
                                                 for: indexPath) as! SongsListTableViewCell
        
        cell.songTitle?.text = songList[indexPath.row].trackName
        cell.songDuration?.text = String(songList[indexPath.row].duration)
        return cell
    }
}

extension AudioListViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let position = indexPath.row
        
        let vc = tableView.dequeueReusableCell(withIdentifier: "SongListCell", 
                                               for: indexPath) as! SongsListTableViewCell
    }
}

我的UITableViewCell代码:

class SongsListTableViewCell: UITableViewCell {
    public var position: Int = 0
    public var isPlaying: Bool = false
    var cellsState: Bool = false
    
    @IBOutlet weak var singleViewCell: UIStackView!
    @IBOutlet weak var songCellView: UIView!
    @IBOutlet weak var songImage: UIImageView!
    @IBOutlet weak var songTitle: UILabel!
    @IBOutlet weak var songDuration: UILabel!
    @IBOutlet weak var makeFavorite: UIButton!
    @IBOutlet weak var useSong: UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        useSong.addTarget(self, action: #selector(SongsListTableViewCell.selectSong(_:)), for: .touchUpInside)
        makeFavorite.addTarget(self, action: #selector(SongsListTableViewCell.makeFavourite(_:)), for: .touchUpInside)
        useSong.isHidden = true
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        useSong.layer.cornerRadius = 6
    }
    
    @IBAction func makeFavourite(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if sender.isSelected {
            makeFavorite.setImage(UIImage(systemName: "star.fill"), for: .normal)
        } else {
            makeFavorite.setImage(UIImage(systemName: "star"), for: .normal)
        }
    }
}

这是现在的样子:

这是我的歌曲列表视图:

你好 Tyler 我在 UITableViewDelegate 中使用带有 didSelectRowAt 和 didDeselectRowAt 的函数来显示和隐藏,并且不隐藏为什么我单击 Cell 中的组件,该函数将检查索引路径中的行并调用函数 hide/show

import UIKit

class ShowHideTableviewVC: UIViewController {
    
    @IBOutlet weak var tableview: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableview.dataSource = self
        self.tableview.delegate = self
    }
    
}

extension ShowHideTableviewVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        5
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ShowHideCell", for: indexPath) as! ShowHideCell
        return cell
    }
}

extension ShowHideTableviewVC: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? ShowHideCell {
            cell.showButton(show: true)
        }
    }
    
    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? ShowHideCell {
            cell.showButton(show: false)
        }
    }
}

这是单元格中的代码,它将隐藏按钮组件并使高度为零,因为我们要隐藏按钮组件

import UIKit

class ShowHideCell: UITableViewCell {
    
    @IBOutlet weak var buttonx: UIButton!
    @IBOutlet weak var heightButton: NSLayoutConstraint!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        showButton(show: false)
    }
    
    
    
    func showButton(show: Bool) {
        if show {
            heightButton.constant = 30
            buttonx.isHidden = !show
        } else {
            heightButton.constant = 0
            buttonx.isHidden = !show
        }
    }
}

当您点击按钮组件内部单元格时,它不会隐藏按钮(播放按钮),因为它使用了 UITableViewDelegate 中的选择