1
votes

I'm new in iOS development. I hope to get help and also apologize if my question is too basic. I'm just getting used to UITableViewController and Delegate, DataSource.

I have the FirstViewController like the photoenter image description here

When user touch inside the segmentedController the TableViewCell will reload data to show the movies are showing and upcoming movies(Just a simple movie booking seats app for my learning purpose).

enter image description here

I see a problem with segues to multiple views.
When user select the cell.
How can I assign the identifier for each reload data so that when I click on the cell, the data is passed to another ViewController(E.g: DetailViewUpcoming and DetailViewShowing). Here I have two other viewcontroller to record detailed information about upcoming movies and showing movie s.(image, name, date, and reverse button....etc)

enter image description here

I just need help with ideas if possible. Or code description is very good. Although I tried to implement segue in function: tableview (_tableView: UITableView, didSelectRowAt indexPath: IndexPath) but my program did not execute as expected. Because I have trouble with segmentedControll (I thin the problem is here)

I have read this post before

Thank you for all

3
Why do you say "tableview (_tableView: UITableView, didSelectRowAt indexPath: IndexPath) but my program did not execute as expected". what should be the expected output that you dont see on screen? How do you expect the segmented control to affect the output?iOSer
the above post that you have read should work for you. Could you please add your code which you are using.Daljeet
Because when segment switched. The cell will reload. DataSouce in the cells after reload are not in same array(I stored data in two arrays). So I don't know how to identify segue to the ViewController after segment switched. Any idea bro? Thank you.sycoi001
When switching segments adjust a boolean variable on top of the class so you can know if you're in the first segment or in the second one. And in tableview (_tableView: UITableView, didSelectRowAt indexPath: IndexPath) you check if you're in first or second segment.DionizB

3 Answers

2
votes

The logic that you need to implement is:

  1. Check the the segmentedIndex to know whether the user wants to see Upcoming or Showing.
  2. Load the tableView with movies per step 1.
  3. When the user taps on cell, use the segmentedIndex to identify whether you should a movie from the Upcoming or Showing array was chosen.
  4. 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.

1
votes

I am assuming the following for your question :

  1. You have an array of strings to populate your tableView
  2. You just need to pass that String to the next View Controller

Now, before sending the data( or String) to next View Controller, you need to check 2 values

  1. Which Segmented Control option was selected when the user selected the cell.

  2. The Index Path of the selected cell


SourceViewController.swift

var upcomingMovies = ["A","B","C"]
var nowShowingMovies = ["D","E","F"]

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:
        cell.textLabel.text = upcomingMovies[indexPath.row]

     case 1:
        cell.textLabel.text = nowShowingMovies[indexPath.row]

     default:
        break
    }

    return cell  
 }

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       switch segmentedControl.selectedSegmentIndex {

        case 0: //We check the *first* value here
          let destinationVCObject = self.storyboard?.instantiateViewController(withIdentifier: "destinationVC") as! DestinationViewController

          //We check the *second* value here
          destinationVCObject. movieName = upcomingMovies[indexPath.row] 

          self.navigationController?.pushViewController(destinationVCObject, animated: true)

        case 1: //We check the *first* value here
          let destinationVCObject = self.storyboard?.instantiateViewController(withIdentifier: "destinationVC") as! DestinationViewController

          //We check the *second* value here
          destinationVCObject. movieName = nowShowingMovies[indexPath.row] 

          self.navigationController?.pushViewController(destinationVCObject, animated: true)

        default:
           break

     }
  }


@IBAction func segmentedTapped(_ sender: Any) {
    myTableView.reloadData()
}

DestinationViewController.swift

class DestinationViewController:UIViewController {

@IBOutlet weak var movieNameLabel:UILabel!

var movieName:String?

override func viewDidLoad() {
    super.viewDidLoad()

    movieNameLabel.text = movieName
   }
}
0
votes

I have solved the problem with segue by two functions:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "segueIdentifier1" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let destinationController = segue.destination as! UpComingDetailView
                destinationController.moVieName = ArrayOfUpcomingMovieList[indexPath.row]
            }
        }
        else if segue.identifier == "segueIdentifier2" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let destinationController = segue.destination as! NowShowingDetail
                destinationController.moVieName = ArrayOfNowShowingMoVieList[indexPath.row]
            }
        }
    }

On didSelectRow

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch segment.selectedSegmentIndex {
        case 0:
            performSegue(withIdentifier: "segueIdentifier1", sender: indexPath)
            print("segue to DetailViewController1")
        case 1:
            performSegue(withIdentifier: "segueIdentifier1", sender: indexPath)
            print("segue to DetailViewController2")
        default:
            break
        }
    }