Swift - 如何从 UITableViewCell 转到另一个 UITableViewCell?
Swift - How go to another UITableViewCell from UITableViewCell?
我有一个 UIViewController,它根据屏幕上的所需选项实例化一些 UITableViewCell。这些选项之一是 "instructions",我创建了一个 .XIB 文件来在屏幕上显示说明。但是,在此屏幕上,我有一个按钮可以显示更多说明(在另一个屏幕上)。因此,我使用说明创建了另一个 .XIB 文件,我需要从第一个说明屏幕转到那里。我该怎么做?
这是我的代码:
import UIKit
class FirstInstructionsCell: UITableViewCell {
@IBOutlet weak var showSecondInstructions: UIButton!
var secondInstructionsView: SecondInstructions!
static let FIRST_INSTRUCTIONS_CELL_IDENTIFIER = "FirstInstructionsCell"
@IBAction func showSecondInstructionsTapped(_ sender: UIButton) {
print("Here I need to go to SecondInstructions screen")
}
}
修改后,我的UIViewController:
import UIKit
class FirstInstructionsViewController: UIViewController, FirstInstructionsCellDelegate {
func goSecondScreen() {
presentViewController(SecondInstructions, animated: true, completion: nil)
// Here I have an error: Cannot convert value of type 'SecondInstructions.Type' to expected argument type 'UIViewController'
// 'SecondInstructions' is a UITableViewCell
}
@IBOutlet weak var confirmButton: UIButton!
@IBOutlet weak var tableView: UITableView!
var withdrawalCode: Int!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER, bundle: nil), forCellReuseIdentifier: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER)
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 470.0
}
//MARK: - Actions
@IBAction func onConfirmClicked(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
}
// MARK: - UITableViewDelegate
extension FirstInstructionsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 13.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 142.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return PayPaxxHeaderView.loadFromNib(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 142.0)) as! PayPaxxHeaderView
}
}
// MARK: - UITableViewDataSource
extension FirstInstructionsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER, for: indexPath) as! FirstInstructionsCell
cell.fillCell(self.withdrawalCode)
cell.delegate = self
return cell
}
}
据我了解,您需要转到另一个屏幕。为什么不使用 protocol
呢?
import UIKit
protocol FirstInstructionsCellDelegate {
func goToSecondScreen()
}
class FirstInstructionsCell: UITableViewCell {
@IBOutlet weak var showSecondInstructions: UIButton!
var secondInstructionsView: SecondInstructions!
var delegate : FirstInstructionsCellDelegate?
static let FIRST_INSTRUCTIONS_CELL_IDENTIFIER = "FirstInstructionsCell"
@IBAction func showSecondInstructionsTapped(_ sender: UIButton) {
print("Here I need to go to SecondInstructions screen")
delegate?.goToSecondScreen()
}
}
然后,在你的 cellForRow(at:indexPath:)
中你可以说(出队后):
cell.delegate = self
您的 viewController
需要实施它:
class FirstInstructionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FirstInstructionsCellDelegate {
然后,在该视图控制器中,实现所述功能:
func goToSecondScreen() {
let story = UIStoryboard(named: "SecondScreen", bundle: nil)
let viewController = story.instantiateViewController(withIdentifier: "SecondScreen")
self.navigationController?.pushViewController(viewController, animated: true)
// or
self.present(viewController, animated: true, completion: nil)
}
注意:
您需要在界面生成器中设置故事板标识符。
我有一个 UIViewController,它根据屏幕上的所需选项实例化一些 UITableViewCell。这些选项之一是 "instructions",我创建了一个 .XIB 文件来在屏幕上显示说明。但是,在此屏幕上,我有一个按钮可以显示更多说明(在另一个屏幕上)。因此,我使用说明创建了另一个 .XIB 文件,我需要从第一个说明屏幕转到那里。我该怎么做?
这是我的代码:
import UIKit
class FirstInstructionsCell: UITableViewCell {
@IBOutlet weak var showSecondInstructions: UIButton!
var secondInstructionsView: SecondInstructions!
static let FIRST_INSTRUCTIONS_CELL_IDENTIFIER = "FirstInstructionsCell"
@IBAction func showSecondInstructionsTapped(_ sender: UIButton) {
print("Here I need to go to SecondInstructions screen")
}
}
修改后,我的UIViewController:
import UIKit
class FirstInstructionsViewController: UIViewController, FirstInstructionsCellDelegate {
func goSecondScreen() {
presentViewController(SecondInstructions, animated: true, completion: nil)
// Here I have an error: Cannot convert value of type 'SecondInstructions.Type' to expected argument type 'UIViewController'
// 'SecondInstructions' is a UITableViewCell
}
@IBOutlet weak var confirmButton: UIButton!
@IBOutlet weak var tableView: UITableView!
var withdrawalCode: Int!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UINib(nibName: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER, bundle: nil), forCellReuseIdentifier: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER)
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 470.0
}
//MARK: - Actions
@IBAction func onConfirmClicked(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
}
// MARK: - UITableViewDelegate
extension FirstInstructionsViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 13.0
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 142.0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return PayPaxxHeaderView.loadFromNib(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 142.0)) as! PayPaxxHeaderView
}
}
// MARK: - UITableViewDataSource
extension FirstInstructionsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: FirstInstructionsCell.FIRST_INSTRUCTIONS_CELL_IDENTIFIER, for: indexPath) as! FirstInstructionsCell
cell.fillCell(self.withdrawalCode)
cell.delegate = self
return cell
}
}
据我了解,您需要转到另一个屏幕。为什么不使用 protocol
呢?
import UIKit
protocol FirstInstructionsCellDelegate {
func goToSecondScreen()
}
class FirstInstructionsCell: UITableViewCell {
@IBOutlet weak var showSecondInstructions: UIButton!
var secondInstructionsView: SecondInstructions!
var delegate : FirstInstructionsCellDelegate?
static let FIRST_INSTRUCTIONS_CELL_IDENTIFIER = "FirstInstructionsCell"
@IBAction func showSecondInstructionsTapped(_ sender: UIButton) {
print("Here I need to go to SecondInstructions screen")
delegate?.goToSecondScreen()
}
}
然后,在你的 cellForRow(at:indexPath:)
中你可以说(出队后):
cell.delegate = self
您的 viewController
需要实施它:
class FirstInstructionViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, FirstInstructionsCellDelegate {
然后,在该视图控制器中,实现所述功能:
func goToSecondScreen() {
let story = UIStoryboard(named: "SecondScreen", bundle: nil)
let viewController = story.instantiateViewController(withIdentifier: "SecondScreen")
self.navigationController?.pushViewController(viewController, animated: true)
// or
self.present(viewController, animated: true, completion: nil)
}
注意:
您需要在界面生成器中设置故事板标识符。