简体   繁体   English

Swift 中的 didSelectRowAt Function 上的 Switch 语句

[英]Switch Statement on didSelectRowAt Function in Swift

I am using a table view and has a label in its cell.我正在使用表格视图,并且在其单元格中有一个label The thing I want is to change the cell label on every tap of a cell.我想要的是在每次点击单元格时更改单元格 label。 Here is my code:这是我的代码:

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

 switch indexPath {
            
        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"
        }

} 

Now if I use:现在,如果我使用:

switch indexPath.row {...}切换 indexPath.row {...}

only case 1 works.只有案例 1 有效。 And if I use only:如果我只使用:

switch indexPath {...}切换 indexPath {...}

It gives me the following error:它给了我以下错误:

Expression pattern of type 'Int' cannot match values of type 'IndexPath' “Int”类型的表达式模式与“IndexPath”类型的值不匹配

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?有没有人有一个解决方案,如果我第一次点击一个单元,案例 1 应该工作,如果我再次点击它,案例 2 应该工作?

You need to save the current tap with an instance variable as indexPath.row will remain zero您需要使用实例变量保存当前点击,因为indexPath.row将保持为零

let arr = ["first time label changes","second time label changes","change the label"] 
var currentIndex = 0 // you can make it 1 initially in case index 0 is pre-assigned 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    guard currentIndex < 3 else { return } // prevent overflow taps
    let cell = tableView.cellForRow(at: indexPath) 
    cell.cellLabel.text = arr[currentIndex]
    currentIndex += 1
} 

Another good practice also is to only refresh cell at didSelectRowAt另一个好的做法是只在didSelectRowAt刷新单元格

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    guard currentIndex < 2 else { return } // prevent overflow  
    currentIndex += 1
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->  UITableViewCell {  
    let cell = //  
    cell.cellLabel.text = arr[currentIndex]
    return cell
} 

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如果您在触摸它 3 次时需要“第一”

 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"
  }
} 
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如果你需要多选写tableView.allowMultipleSelection = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM