Hello, I was able to display multiple cells using tableView in Xcode.
And I would like to change the screen transition destination for each cell.
If you tap a cell using the Navigation Controller, the screen will change, but no matter which cell you choose, it will transition to the same screen.
From here, how do I change the screen transition destination for each cell?
The following programs are written in viewController:
class ViewController:UIViewController, UITableViewDelegate, UITableViewDataSource{
@IBOutlet weak variableView:UITableView!
letmath: [String] = ["Mathematics I", "Mathematics A", "Mathematics II", "Mathematics B", "Mathematics III", "Mathematics C"]
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) - > Int{
return path.count
}
functableView(_tableView:UITableView, cellForRowAtindexPath:IndexPath) - >UITableViewCell{
letcell:UITableViewCell=tableView.dequeueReusableCell(withIdentifier: "beginnerCell", for:indexPath)
cell.textLabel!.text=math [indexPath.row]
return cell
}
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view, typically from anib.
}
override funcdidReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
How do I change the screen transition destination for each cell?
When each cell is tapped, the following UITableViewDelegate Protocol methods are called:
functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
}
The indexPath
argument determines which cell was tapped, which branches the process.
Here are some specific code examples:
functableView(_tableView:UITableView, didSelectRowAtindexPath:IndexPath){
switch indexPath.row {
case0:
// Cell-Tap Processing of Mathematics I
// The following is an example of a screen transition of mathematics I in Segue.
self.performSegue(withIdentifier: "toMath1View", sender:nil)
case1:
// When you're in math A
...(Same as above, the process of transitioning to the screen of mathematics A)
case2:
// Math II Self-Tap Time
...(Transition to Mathematics II screen as above)
case3:
// When you're in math B, when you're in the cell tap,
...(Transition to math B screen as above)
case4:
// Math III When Self-Tap
...(Transition to Mathematics III screen as above)
case5:
// When you self-tap math C
...(Same as above, the process of transitioning to the screen of mathematics C)
}
}
Also, it is not good to write a direct value like case0
, considering the serviceability of the code, so I think it would be good to define enum
as follows.
// Mathematics Subjects
///
/// -math1: Mathematics I
/// -mathA: Mathematics A
/// -math2 - Mathematics II
/// -mathB: Mathematics B
/// -math3: Mathematics III
/// -mathC: Mathematics C
enum MathCourse:Int{
case path1 = 0
case pathA
case path2
case pathB
case path3
case pathC
/// Transition destination segment identifier
varseugeIdentifier: String {
switchself{
case.math1:
return "toMath1"
case.mathA:
return "toMathA"
case.math2:
return "toMath2"
case.mathB:
return "toMathB"
case.math3:
return "toMath3"
case.mathC:
return "toMathC"
}
}
Use it as follows:
switch indexPath.row {
caseMathCourse.math1:
// Cell-Tap Processing of Mathematics I
// The following is an example of a screen transition of mathematics I in Segue.
self.performSegue (withIdentifier: MathCourse.math1.seugeIdentifier, sender:nil)
caseMathCourse.mathA:
・・・
© 2024 OneMinuteCode. All rights reserved.