【问题标题】:How to get the indexPath of tableView and use the indexPath on the metod prepare(for segue: )如何获取tableView的indexPath并在metod prepare上使用indexPath(for segue:)
【发布时间】:2018-12-19 18:20:22
【问题描述】:

我有一个 Tableview 显示一个有 7 行的部分,我想要做的是当我按下第 0 行时,我将它发送到另一个视图,表中的所有信息都是通过代码使用修复加载的,我的问题是如何在“prepare()”方法中获取 indexPath 以及如何以编程方式完成 segue。

这是我的课,我的 TableView 中有所有代码:

//
//  ControlTablaPrincipalTableViewController.swift
//  Seccion 15
//
//  Created by Barbatos on 7/10/18.
//  Copyright © 2018 Seccion 15. All rights reserved.
//

import UIKit

class ControlTablaPrincipalTableViewController:UITableViewController {

    var titulos : [String] = ["Caja de ahorro","Blog de la Seccion 15","Iniciar Sesion","Galeria de eventos","Convenios", "Ubicacion", "Contactanos"]

    var imagenes : [UIImage] = [#imageLiteral(resourceName: "caja"),#imageLiteral(resourceName: "blog"),#imageLiteral(resourceName: "sesion"),#imageLiteral(resourceName: "galeria"),#imageLiteral(resourceName: "convenio"),#imageLiteral(resourceName: "ubicacion"),#imageLiteral(resourceName: "contacto")]

    override func viewDidLoad() {

        super.viewDidLoad()

        self.tableView.dataSource = self
        self.tableView.delegate = self

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return titulos.count

    }

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        cell?.imageView!.image = imagenes[indexPath.row]
        cell?.textLabel?.text = titulos[indexPath.row]

        return cell!
    }

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

        let alert = UIAlertController(title: "Celda seleccionada", message: "Se selecciono la celda \(indexPath.row)",preferredStyle: .alert)

        let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)

        alert.addAction(okAction)
        self.present(alert, animated: true, completion: nil)
    }

这是我的 didSelectRowAt 方法,我在其中编写了一个警报,它将显示我正在编程的单元格:

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

        let alert = UIAlertController(title: "Celda seleccionada", message: "Se selecciono la celda \(indexPath.row)",preferredStyle: .alert)

        let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)

        alert.addAction(okAction)        
        self.present(alert, animated: true, completion: nil)   
   }

这是 prepare 方法,我在其中尝试恢复选定的单元格并编写一个 if 以便能够使用我的 segue:

overrider func prepare(for segue: UIStoryboardSegue, sender: Any?){

    if let indexPath = tableView.indexPathForSelectedRow{
        let selectedRow = indexPath.row
            if(selectedRow == 0){
               segue.destination as? CajadeAhorroViewController 
            }
    }

}

我的segue的标识符是“caja”:

【问题讨论】:

  • 不相关但在 UITableViewController 的子类中 datasourcedelegate 隐式连接。代码中的两行是多余的。并且不要使用多个数组作为数据源。 Swift 是一种面向对象的语言。使用自定义类或结构。如果将 segue 连接到 cell 而不是视图控制器,则单元格将在 prepare(for 中的 sender 参数中传递,而您不需要 didSelect
  • @vadian 所以你推荐我使用像 MVC 这样的东西?
  • 是的,我愿意。它更可靠。

标签: ios swift3 xcode8


【解决方案1】:

我认为您只想在 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 函数中执行 segue...

override func tableView(_ tableView: UITableView, 
           didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {
    case 0:
        performSegue(withIdentifier: "caja", sender: nil)
    case 1:
        performSegue(withIdentifier: "whatever you set this identifier to", sender: nil)
    // cases for all the different rows...
    }
}

如果您只有一个 segue,并且您需要切换视图控制器的变量,它会根据所选单元格以不同方式显示

overrider func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if let indexPath = tableView.indexPathForSelectedRow,
       let destination = segue.destination as? CajadeAhorroViewController {
        switch indexPath.row {
        case 0:
            // destination is the "CajadeAhorroViewController", set its properties for how you want it.
        case 1: 
        // ... all 7 cases ...
        }
    }
}

【讨论】:

  • 我已经使用了你的解决方案,它给了我这个错误:当一个存在的转换或演示发生时;导航堆栈不会更新
  • 对不起,我看到了问题,是因为显示的警报
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多