TableView with Segues to Multiple Views
The logic that you need to implement is:
- Check the the segmentedIndex to know whether the user wants to see Upcoming or Showing.
- Load the tableView with movies per step 1.
- When the user taps on cell, use the segmentedIndex to identify whether you should a movie from the Upcoming or Showing array was chosen.
Get the ID of that movie and send it to the respective DetailsViewController. Now that you have the movieID, use it to request the movie details from your database.
func tableView(_ tableView:UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as? TableViewCell else { return UITableViewCell() }
switch segmentedControl.selectedSegmentIndex {
case 0:
//handle the set up of Upcoming cells
case 1:
//handle the setup of Showing cells
default:
break
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch segmentedControl.selectedSegmentIndex {
case 0:
let DetailViewUpcoming = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewUpcoming") as! DetailViewUpcoming
DetailViewUpcoming.init(forMovie: upcomingArray[indexPath.row].movieID)
self.navigationController?.pushViewController(DetailViewUpcoming, animated: true)
case 1:
let DetailViewShowing = self.storyboard?.instantiateViewController(withIdentifier: "DetailViewShowing") as! DetailViewShowing
DetailViewShowing.init(forMovie: showingArray[indexPath.row].movieID)
self.navigationController?.pushViewController(DetailViewShowing, animated: true)
default:
break
}
}
@IBAction func segmentedTapped(_ sender: Any) {
tableView.reloadData()
}
Inside DetailViewUpcoming
and DetailViewShowing
declare a variable for the movieID and add an initialiser
var movieID: String?
init(movieID:String){
self.movieID = movieID
}
Now when you load the view use the above movieID
to request the movie details and assign them to properties as you see fit.
Storyboard TableView with Segues to Multiple Views
The methodoverride func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
is called when an item is deselected.
To call a method when an item is selected, change the method name to override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
IOS StoryBoard multiple Segue's from a TableCell
Don't try to hook up the Segues to a tableviewcell in this case. Hook them up to the View Controller itself.
How to set multiple segue ViewController in TableViewController?
- Make a segue from the controller not from the cell
- call a didselectrow atindexpath method of tableview
call a prepare for segue with the identifier you have set for different segues
you are done
Can you have two Segues from different View Controllers going to one View Controller Swift 3
Yes, you are allowed to have several segues going from different View controllers to a single View controller.
The problem is you have either not set up an identifier for that particular view controller or you have a typo in the identifier in your code.
How to use Storyboards with multiple segues to one view controller?
Every ViewController should have their own seque. To create, press "Ctrl" and move to the ViewController you want
And every ViewController should implement "prepareForSegue"
- (void)prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender {
if ([segue.destinationViewController isKindOfClass:[MyDestinationVC class]]) {
//do something
//Example
MyDestinationVC *screen = (MyDestinationVC *)segue.destinationViewController;
screen.someValue = self.sendedValue;
}
}
How do I use a table view to go to different view controllers?
Yes, didSelectRowAt
function is the best way to identify which cell or row is selected at the time.
didSelectRowAt
Function should be like belowfunc tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
performSegue(withIdentifier: "FirstViewController", sender: nil)
} else if indexPath.row == 1 {
performSegue(withIdentifier: "SecondViewController", sender: nil)
}
}Segue Prepare Function Should be Like Below
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "FirstViewController" {
let vc = segue.destination as? FirstViewController
} else if segue.identifier == "SecondViewController" {
let vc = segue.destination as? SecondViewController
}
Hope, It Helps :)
Related Topics
Unsafemutablepointer in Swift as Replacement for Properly Sized C Array in Obj-C
Understanding the Userdefaults Register Method
App Crashes When Playing Audio on iOS13.1
Fbsdkapplicationdelegate Application Openurl:Sourceapplication:Annotation Deprecated
Handle Swiftui and Corelocation with Mvvm-Pattern
Updating Uitableview with Multiple Sections from Rlmresults.Observe()
How to Make Firebase Database Data the Data Source for Uicollection View
Drawing a 3D Arc and Helix in Scenekit
Attaching Audiounit Effects to Scnaudiosource Nodes in Scenekit
How to Restrict the Type That a Function Throws in Swift
Swift - 'Bool' Is Not a Subtype of 'Void'
Early Return from a Void-Func in Swift
Result Values in '? :' Expression Have Mismatching Types 'Some View' and '...'
Get Header Data from a Request Response in Swift
Hstack with Sf Symbols Image Not Aligned Centered
Undefined Behavior, Or: Does Swift Have Sequence Points
Create Skscene Subclasses Programmatically, Without Size Info