编辑-UITableView单元格按钮动作委托-Swift 4

时间:2019-07-06 08:49:31

标签: swift xcode uitableview uibutton delegates

编辑-

我有8个不同的ViewController 并希望每个单元都将其推送,因此应该转到ViewControllers 1等。

此代码对我来说很好,可以使用segue委托给另一个视图控制器,希望该代码对您有帮助

编辑-

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isPurchased() {
        return freeQuotes.count
    }
    return freeQuotes.count + 1
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    if indexPath.row < freeQuotes.count {

        cell.textLabel?.text = freeQuotes[indexPath.row]
        cell.textLabel?.font = UIFont(name: (cell.textLabel?.font.fontName)!, size:20)
        cell.textLabel?.textColor = cell.textLabel?.textColor = colorLiteral
        cell.accessoryType = .disclosureIndicator


    } else {

        cell.textLabel?.text = ""
        cell.textLabel?.font = UIFont(name: (cell.textLabel?.font.fontName)!, size:20)
        cell.textLabel?.textColor = colorLiteral
        cell.accessoryType = .disclosureIndicator

    }
    return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.row == freeQuotes.count {

    }
     performSegue(withIdentifier: segueIndenidentity[indexPath.row], sender: self)

}

1 个答案:

答案 0 :(得分:2)

它将与开关盒一起处理:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

   switch indexPath.row {
    case 1:
        // go to first view controller
    case 2:
        // go to second view controller
    case 3:
        // go to third view controller
    case 4:
        // go to fourth view controller
    case 5:
        // go to fifth view controller
    default:
        print("no vc")
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? ""){
    case "MySegueIdentifier":

        guard let selectedCell = sender as? MainTableViewCell
            else{
                fatalError("Unexpected Sender \(String(describing: sender))")
        }
        guard let indexPath = mainTableView.indexPath(for: selectedCell)
            else{
                fatalError("The selected cell is not being displayed by the table")
        }

        switch indexPath.row {
        case 0:
            let nextVC = segue.destination as! FirstViewController
        case 1:
            let nextVC = segue.destination as! SecondViewController
        default:
            print("Nothing")
        }

    default:
        fatalError("Unexpected Segue Identifier \(String(describing: segue.identifier))")
    }
}