skip to Main Content

I am using a table view and has a label in its cell. The thing I want is to change the cell label on every tap of a cell. I know that switch statement doesn’t work on indexPath. I want my cases 1 and 2 to work.

Here is my code:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 
  let cell = tableView.cellForRow(at: indexPath)

 switch Value {
            
        case 1:
            
            cell.cellLabel.text = "first time label changes"
        case 2:
            
            cell.cellLabel.text = "second time label changes"
            
        default:
            cell.cellLabel.text = "change the label"
        }

} 

Does anyone have a solution that if I tap a cell first time case 1 should work and if I tap it again case 2 should work?

2

Answers


  1. try my code

    var count = 0
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    count += 1
    let cell = tableView.cellForRow(at: indexPath)
    switch count {   
        case 1:
            cell.cellLabel.text = "first time label changes"
        case 2:
            cell.cellLabel.text = "second time label changes"
            
        default:
            cell.cellLabel.text = "change the label"
        }
    } 
    

    if you need "first" when you touch it 3 times

     var count = 0
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
       count += 1
       if count > 2 {
        count = 1
       }
       let cell = tableView.cellForRow(at: indexPath)
       switch count {   
       case 1:
         cell.cellLabel.text = "first time label changes"
       case 2:
         cell.cellLabel.text = "second time label changes"
       default:
         cell.cellLabel.text = "change the label"
      }
    } 
    
    Login or Signup to reply.
  2. var selectedRows = [Int]()
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
           
                selectedRows.append(indexPath.row)
                print(selectedRows)
                self.tableView.reloadData()
            }
        }
        
        func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
           
                if let index = selectedRows.firstIndex(of: indexPath.row) {
                    selectedRows.remove(at: index)
                }
                print(selectedRows)
               self.tableView.reloadData()
                if selectedRows.isEmpty{
                   
                }
            }
        }
    
    class TableViewCell: UITableViewCell{
     override func setSelected(_ selected: Bool, animated: Bool) {
                super.setSelected(selected, animated: animated)
                if (selected) {
                    print("selected")
                    
                    selectImage.image = UIImage(named:"secondImage")
                  } else {
                      selectImage.image = UIImage(named:"secondImage")
                  }
            }
    }
    

    if you need multiple selection write tableView.allowMultipleSelection = true

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search