swift - 编辑- UITableView 单元格按钮操作委托(delegate)- Swift 4

标签 swift xcode uitableview uibutton delegates

编辑-

我有 8 个不同的 ViewController 并希望每个单元格都推送它,所以应该转到 ViewControllers 1,等等。

这段代码很适合我使用 segue 委托(delegate)给另一个 View Controller ,希望这段代码对你有帮助

编辑-

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)

}

最佳答案

它将处理 switch case :

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

关于swift - 编辑- UITableView 单元格按钮操作委托(delegate)- Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56912557/

相关文章:

ios - 展开时 SKScene 到 SKScene 零问题

iOS mapview 使用 word 搜索位置

ios - 单元格中带有 UIWebView 的 UITableView 性能不佳

ios - 具有预定义属性的 UITableView 子类

ios - 类对于键 staticTexts 不符合键值编码

ios - 如何快速在 UICollectionView 中添加分页?

swift - NSCollectionView 如何在没有 item 的情况下绘制文本?

ios - 在 UITableViewCell Xcode StoryBoard 中调整标题

Swift 弱惰性变量不会编译

ios - 将参数添加到 UILabel 初始值设定项