【发布时间】: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的子类中datasource和delegate隐式连接。代码中的两行是多余的。并且不要使用多个数组作为数据源。 Swift 是一种面向对象的语言。使用自定义类或结构。如果将 segue 连接到 cell 而不是视图控制器,则单元格将在prepare(for中的sender参数中传递,而您不需要didSelect。 -
@vadian 所以你推荐我使用像 MVC 这样的东西?
-
是的,我愿意。它更可靠。