Hello,
Im trying to open Each vc with each Table View Cell tapped. All I have manged to do is to open one VC with all of Table View Cells. I would like each cell to have open each VC. Here is my code:
RoutesController
import UIKit
class TrasyController: UIViewController,UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return routes.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath) as! TableTableViewCell
cell.photo.image=self.routesImages[indexPath .row]
cell.label.text=self.routes[indexPath .row]
cell.label_car.text=self.czas_auto[indexPath .row]
cell.label_bike.text=self.czas_rower[indexPath .row]
cell.label_walk.text=self.czas_pieszo[indexPath .row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "1", sender: self)
} //Here I can perform just one VC form all cells, I need each cell to open each VC (cell with name "one" opens "route1_vc"; cell with name "two" opens "route2_vc" etc.)
@IBOutlet weak var tableView: UITableView!
let routes = [("one"),("two"),("three")]
let routesImages = [UIImage(named: "1.jpeg"), UIImage(named: "2.jpeg"), UIImage(named: "3.jpeg")]
let czas_auto = [("2h"),("75m"),("2h50m")]
let czas_rower = [("2h"),("1h25n"),("3h50m")]
let czas_pieszo = [("2h"),("2h"),("4h50m")]
let vc = ["route1_vc","route2_vc","route3_vc"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
TableTableViewCell:
import UIKit
class TableTableViewCell: UITableViewCell {
@IBOutlet weak var photo: UIImageView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var label_car: UILabel!
@IBOutlet weak var label_bike: UILabel!
@IBOutlet weak var label_walk: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Thank you in advance for your help.
You do not yell how your cell type is defined. Is it a subclass of UITableViewCell ? Or a plain UITableViewCell ?
I assume you have defined a subclass to have specific properties, as name.
You could do in several ways:
- Create as many segues (from the VC itself) to the different destinations (route1_vc, route2_vc…) and use them:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath) as? YourCellType {
switch cell.name {
case "one": performSegue(withIdentifier: "1", sender: self)
case "two": performSegue(withIdentifier: "2", sender: self)
default: break
}
}
} //Here I can perform just one VC form all cells, I need each cell to open each VC (cell with name "one" opens "route1_vc"; cell with name "two" opens "route2_vc" etc.)
- Go directly to the VC:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath) as? YourCellType {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var destViewController : UIViewController?
switch cell.name {
case "one": destViewController = storyboard.instantiateViewController(withIdentifier: "route1_vc")
case "two": destViewController = storyboard.instantiateViewController(withIdentifier: "route2_vc")
default: return
}
if destViewController != nil {
self.present(destViewController!, animated: true, completion: nil)
}
}
} //Here I can perform just one VC form all cells, I need each cell to open each VC (cell with name "one" opens "route1_vc"; cell with name "two" opens "route2_vc" etc.)