【问题标题】:Edit- UITableView cell buttons action delegate- Swift 4编辑- UITableView 单元格按钮动作委托- Swift 4
【发布时间】:2019-07-06 08:49:31
【问题描述】:

编辑-

我有 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)

}

【问题讨论】:

标签: swift xcode uitableview uibutton delegates


【解决方案1】:

它将与开关盒一起处理:

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))")
    }
}

【讨论】:

  • 你能展示如何在代码中使用prepare segue吗?没有让它正常工作
猜你喜欢
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
相关资源
最近更新 更多