为 UITableView 中的每一行启用独特的操作

Enable unique actions for each row in UITableView

我有一个 7 行的小表格视图。我想为每一行的按下启用不同的操作。

例如,索引 0 处的行将显示另一个 VC(不传递数据)。索引 0 处的行将打开一个网站 URL,下一个可能是共享功能等。

我最大的问题是第一个将用户发送给另一个 VC 的情况。主要是因为我不知道是否应该启用 segue。对于所有情况,我需要能够分别为每一行指定一个函数,因此我需要指定正确的索引(0、1、2 等)。

我该怎么做Swift?

这是您需要的代码框架:

class ViewController: UITableViewController {

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 2 }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = String(describing: indexPath)
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            print("Do action for row 0")
        } else if indexPath.row == 1 {
            print("Do action for row 1")
        }
    }
}