Reputation: 33
i have label 'Now Playing' and 'Image' that should be display on selected cell, then as i select other cell the prev selected should be clear now.
Code i have written:
func didSelectedRow(indexPath: IndexPath) {
guard let cell = tblMusicPro.cellForRow(at: indexPath) as? musicTableViewCell else {
return
}
if indexPath == currentIndex {
cell.lblTagsPro.text = "Now Playing"
cell.imgPlaying.alpha = 1
} else {
cell.lblTagsPro.text = ""
cell.imgPlaying.alpha = 0
}
}
Upvotes: 0
Views: 5241
Reputation: 4570
For this take one array for store IndexPath, then check it at cellForRowAt
as follow this code, add IndexPath
into an array when select cell and remove if already exist in an array then reload tableview cell to update value.
var selectedCells:[IndexPath] = []
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tblMusicPro.cellForRow(at: indexPath) as? musicTableViewCell else {
return
}
if selectedCells.contains(indexPath) {
cell.lblTagsPro.text = "Now Playing"
cell.imgPlaying.alpha = 1
} else {
cell.lblTagsPro.text = ""
cell.imgPlaying.alpha = 0
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if selectedCells.contains(indexPath) {
let getIndex = selectedCells.index(of: indexPath)
selectedCells.remove(at: getIndex)
}else{
selectedCells.append(indexPath)
}
tableView.reloadRows(at: [indexPath], with: true)
}
Upvotes: 1
Reputation: 21
There are a few solutions for this.
Implement TableView delegate method didDeselectRowAt
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)
If you have a custom cell class, specify selection/deselection logic in setSelected
method in the cell class.
override func setSelected(_ selected: Bool, animated: Bool)
Set the label property if you have the previouslySelectedIndexPath. Call this method from didSelectRowAt
delegate method.
func removeNowPlayingText() {
if let cell = tableView.cellForRow(at: previouslySelectedIndexPath) as? MusicTableViewCell {
cell.lblTagsPro.text = ""
cell.imgPlaying.alpha = 0
}
}
Upvotes: 2
Reputation: 5679
You have changed the state of object of table cell, but it won't be reflected until table cell reloads. You can either reload the whole tableView or reload only 2 cell which are deselecting and selecting.
For this,
Keep a variable in your class to store selected cell index
var selectedCell: IndexPath?
When you click a cell, store indexpath
in this variable and reload the cell. say,
func selectCell(indexPath: IndexPath) {
selectedCell = indexPath
self.tblMusicPro.reloadData()
}
In your cellForRowAtIndexPath
, check if cell is selected or not?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: <cell_identifier>, for: indexPath) as! musicTableViewCell
if indexPath == selectedCell {
cell.lblTagsPro.text = "Now Playing"
cell.imgPlaying.alpha = 1
} else {
cell.lblTagsPro.text = ""
cell.imgPlaying.alpha = 0
}
//
// Rest of your implementation
//
return cell
}
Upvotes: 1