我有一个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)
}
这是准备方法,在该方法中,我尝试恢复选定的单元并编程是否可以使用我的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
}
}
}
我的搜索的标识符是“ caja”:
答案 0 :(得分:1)
我认为您只想在tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
函数中执行搜索...
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 ...
}
}
}