每个项目的 TableView 操作

TableView action per item

我有一个这样的 tableView:

    class ViewController: UIViewController {
            
            @IBOutlet var tableView: UITableView!
            
            let names = ["TEXT1", "TEXT2", "TEXT3"]
            
            
        
            override func viewDidLoad()
            {
                super.viewDidLoad()
                // Do any additional setup after loading the view.
                
                tableView.delegate = self
                tableView.dataSource = self
                 
            }
        }
    
    
    extension ViewControllerMeer: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let name = names[indexPath.row]
        print("You clicked on \(name)")
    }
        
}
 
 extension ViewControllerMeer: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
     
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return names.count
 }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        
        cell.textLabel?.text = names[indexPath.row]
        
        let n = names[indexPath.item]
        switch n {
        case "TEXT1":
            print("s1")
        case "TEXT2":
            print("s2")
        case "TEXT3":
            print("s3")
        default:
            break
        }
        
        return cell
    }
    
    

}

它从 'let names' 中读取项目,目前一切顺利 :) 我现在想添加一些操作,因为现在每个项目都会打印 'You tapped me'。我想到了

  override func tableView(_ tableView: UITableView,
                          didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    if indexPath.row == 0 {
      print("cell1")
    } else if indexPath.row == 1 {
      print("cell2")
    } else if indexPath.row == 2 {
      print("cell3")
    }
  }

唯一的问题是它会产生错误 'Method does not override any method from its superclass'。所以我想这行不通。什么方法比较好?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)前面的override去掉

你可以使用一个大的 if 语句或 switch 语句来对名称做一些事情,或者你可以做类似的事情:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let name = names[indexPath.row]
    print("You clicked on \(name)")
    
    switch indexPath.row {
        case 0:
             //do something
             self.shareApp()
        case 1:
             //do something else
             self.writeReview()
        default:
            break
    }

}

我只是复制了您的代码,基本上向您展示了您是否真的想覆盖 tableview 函数

class ViewController: UITableViewController {

    
    @IBOutlet var tableView: UITableView!
    
    let names = ["TEXT1", "TEXT2", "TEXT3"]
    
    override func viewDidLoad() {
        super.viewDidLoad()    
      }
  }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped me!")
  }

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return names.count
  }

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = names[indexPath.row]
    return cell
  }
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let name = names[indexPath.row]
    print("You clicked on \(name)")
  }
 }