Редактировать - UITableView ячейка кнопки делегат действия - Swift 4 - PullRequest
0 голосов
/ 06 июля 2019

Edit-

У меня есть 8 разных ViewControllers и хотите, чтобы каждая ячейка выдвигала его, поэтому следует перейти к ViewControllers 1 и т. д.

Этот код хорошо подходит для делегирования мне другого контроллера представления с помощью segue, надеюсь, этот код поможет вам

Edit-

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 Ответ

2 голосов
/ 06 июля 2019

Это будет ручка с переключателем:

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))")
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...