Android开发:点击电台列表单元格显示播放图标并隐藏原图标
Hey there! Let's fix this play icon toggle issue for your radio app—super common requirement, and totally solvable with a few key steps.
First, let's recap your core need to make sure we're aligned:
点击电台列表的某一单元格时,在该单元格内显示播放图标,同时隐藏之前显示的播放图标。例如,点击“电台1”开始播放后,在其右侧显示播放图标;当点击另一单元格时,需隐藏原播放图标,在新选中的单元格中显示该图标。
You also shared this unfinished code snippet:
// MARK: - QUERY STATIONS --------------------------------------------------------------- void queryStations(String...
Let's build out the solution around this, including handling table view cell reuse (a critical detail to avoid weird icon glitches):
1. Track the Currently Playing Station
First, add a property to your view controller to keep track of which cell is currently displaying the play icon. If you're using UITableView, we'll store the IndexPath of the active cell:
// Add this to your ViewController class private var currentPlayingIndexPath: IndexPath?
2. Handle Cell Selection Logic
In your table view's didSelectRowAt method, we'll toggle the icons between the old and new selected cells:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // Step 1: Hide the play icon from the previously active cell if let previousPath = currentPlayingIndexPath { if let previousCell = tableView.cellForRow(at: previousPath) as? YourStationCell { previousCell.playIconImageView.isHidden = true } } // Step 2: Show the play icon on the newly selected cell if let currentCell = tableView.cellForRow(at: indexPath) as? YourStationCell { currentCell.playIconImageView.isHidden = false } // Step 3: Update our tracker to the new active cell currentPlayingIndexPath = indexPath // Add your actual radio playback logic here (e.g., using your queryStations data) startPlayingStation(at: indexPath) }
Note: Replace YourStationCell with your actual custom cell class name, and playIconImageView with the name of your play icon UI element in the cell.
3. Fix Cell Reuse Glitches
Table views reuse cells, so we need to ensure the play icon displays correctly when cells are recycled. Update your cellForRowAt method:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "YourStationCellID", for: indexPath) as! YourStationCell // Configure your cell's content (using data from queryStations) let station = yourStationsDataSource[indexPath.row] cell.stationNameLabel.text = station.name // Set play icon visibility based on the current active cell cell.playIconImageView.isHidden = (currentPlayingIndexPath != indexPath) return cell }
4. Handle External Playback State Changes
If your radio can stop playing outside of cell taps (e.g., end of stream, user taps pause), add logic to update the icon state:
func handlePlaybackStopped() { if let activePath = currentPlayingIndexPath { // Hide the icon from the active cell if let activeCell = tableView.cellForRow(at: activePath) as? YourStationCell { activeCell.playIconImageView.isHidden = true } // Reset the active cell tracker currentPlayingIndexPath = nil } }
内容的提问来源于stack exchange,提问作者Frank Eno

