我有一个 Tableview,显示一个包含 7 行的部分,我想要做的是,当我按第 0 行时,我将其发送到另一个 View ,表中的所有信息都是通过使用修复的代码加载的,我的问题是如何在“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)
}
这是准备方法,我尝试恢复所选单元格并编写 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”:
最佳答案
我认为您只想在 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,并且需要切换 View Controller 的变量,它会根据所选单元格的不同显示不同
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 ...
}
}
}
关于ios - 如何获取 tableView 的 indexPath 并在方法准备上使用 indexPath(对于 segue : ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51290042/