Arslan Ahmed
Arslan Ahmed

Reputation: 33

change label text for specific cell of uitableView using swift

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

'Now Playing' and 'Image' should be display on selected cell, then as i select other cell the prev selected should be clear now.

Upvotes: 0

Views: 5241

Answers (3)

AtulParmar
AtulParmar

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

Manu
Manu

Reputation: 21

There are a few solutions for this.

  1. Implement TableView delegate method didDeselectRowAt

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath)

  2. 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)

  3. 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

Ankit Jayaswal
Ankit Jayaswal

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

Related Questions