I have the below code where each item in the UITableView goes to the same view controller screen and I am trying to create a segue to the second view controller:
import UIKit
class ItemsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ItemsBaseCoordinated {
var coordinator: ItemsBaseCoordinator?
private let myArray: NSArray = ["India","Canada","Australia"]
private var myTableView: UITableView!
init(coordinator: ItemsBaseCoordinator) {
super.init(nibName: nil, bundle: nil)
self.coordinator = coordinator
title = "Items"
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
//let barHeight: CGFloat = UIApplication.shared.statusBarFrame.size.height
let barHeight = UIApplication.shared.connectedScenes
.filter {$0.activationState == .foregroundActive }
.map {$0 as? UIWindowScene }
.compactMap { $0 }
.first?.windows
.filter({ $0.isKeyWindow }).first?
.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
let displayWidth: CGFloat = self.view.frame.width
let displayHeight: CGFloat = self.view.frame.height
myTableView = UITableView(frame: CGRect(x: 0, y: barHeight, width: displayWidth, height: displayHeight - barHeight))
myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
myTableView.dataSource = self
myTableView.delegate = self
self.view.addSubview(myTableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
cell.textLabel!.text = "\(myArray[indexPath.row])"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Selected Row \(indexPath.row)")
let nextVC = SecondViewController()
// Push to next view
navigationController?.pushViewController(nextVC, animated: true)
}
}
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemRed
title = "bruh"
}
}
The below video represents what I actually want:
Currently each item goes to the same viewcontroller screen, how do I perform this segue with identifiers because I am not even using storyboards