title: Swift Segment control with Table View Cell Data Passing techniques
body: <p>I want to pass the data from one table view cell to another one table view cell . I want to add those table view cell values to another segment control when user click the add the button . I have two table view with cell in same view controller . I am following delegate to implement it but I facing some issues .</p>
  <p><strong>1. I used the isHide methods to hide the specific table view based on segment control selection , still show the both table views although I wanted to show one and both segment the same data is displaying .</strong></p>
  <p>Here is the code .</p>
  <pre><code>@IBAction func selectSegment(_ sender: UISegmentedControl) {
          
         if sender.selectedSegmentIndex == 0{
             FavouriteTableView.isHidden = true
          setUpUI()
          presenter = MoviePresenter(view: self)
          presenter.getMovies()
             
          }
          
      } 
  </code></pre>
  <p><strong>2. I want add the table view cell values to seconds table view cell when User clicked the button but actually not working .Here is the code for both table view cell .</strong></p>
  <pre><code>protocol CellSubclassDelegate: AnyObject {
      func buttonTapped(cell: MovieViewCell)
  }
  
  class MovieViewCell: UITableViewCell {
      
      weak var delegate:CellSubclassDelegate?
      
      static let identifier = &quot;MovieViewCell&quot;
      
  
      @IBOutlet weak var movieImage: UIImageView!
      @IBOutlet weak var movieTitle: UILabel!
      @IBOutlet weak var movieOverview: UILabel!
      @IBOutlet weak var someButton: UIButton!
      
      
      @IBAction func someButtonTapped(_ sender: UIButton) {
          self.delegate?.buttonTapped(cell: self)
      }
      
      
      
      override func prepareForReuse() {
          super.prepareForReuse()
          self.delegate = nil
      }
      
      func configureCell(title: String?, overview: String?, data: Data?) {
          
          movieTitle.text = title
          movieOverview.text = overview
          
          movieImage.image = nil
          if let imageData = data{
              movieImage.image = UIImage(data: imageData)
             //  movieImage.image = nil
          }
      }
      
  }
  </code></pre>
  <p>Code for second cell.</p>
  <pre><code>protocol CellDelegate: AnyObject {
      func AddFavourite(cell: FavouriteTableViewCell)
  }
  
  class FavouriteTableViewCell: UITableViewCell {
      
      weak var delegate:CellDelegate?
  
      @IBOutlet weak var FavouriteImage: UIImageView!
      
      @IBOutlet weak var FavouritemovieTitle: UILabel!
      
      @IBOutlet weak var FavouritemovieOverview: UILabel!
      
      @IBAction func NewMovie(_ sender: UIButton) {
          self.delegate?.AddFavourite(cell: self)
      }
      
  }
  </code></pre>
  <p>Table view controller code implementation ..</p>
  <pre><code>class MovieViewController: UIViewController, UISearchBarDelegate {
      
      @IBOutlet weak var userName: UILabel!
      @IBOutlet weak var tableView: UITableView!
      @IBOutlet weak var searchBar: UISearchBar!
      @IBOutlet weak var FavouriteTableView: UITableView!
      @IBOutlet weak var segmentControl: UISegmentedControl!
      
      private var presenter: MoviePresenter!
      
      var finalname = &quot;&quot;
      
     var movieTitle = &quot;&quot;
      var movieOverview = &quot;&quot;
      var movieImage : UIImage?
      
      
      override func viewDidLoad() {
          super.viewDidLoad()
  
         userName.text = &quot;Hello: &quot; + finalname
  
         
      }
      private func setUpUI() {
          tableView.dataSource = self
          tableView.delegate = self
      }
      
      
     private func searchBarText() {
          searchBar.delegate = self
      }
      
      @IBAction func selectSegment(_ sender: UISegmentedControl) {
          
         if sender.selectedSegmentIndex == 0{
             FavouriteTableView.isHidden = true
          setUpUI()
          presenter = MoviePresenter(view: self)
          presenter.getMovies()
             
          }
          
      }
  
  extension MovieViewController: MovieViewProtocol {
      
      func resfreshTableView() {
          tableView.reloadData()
      }
      
      func displayError(_ message: String) {
          let alert = UIAlertController(title: &quot;Error&quot;, message: message, preferredStyle: .alert)
          let doneButton = UIAlertAction(title: &quot;Done&quot;, style: .default, handler: nil)
          alert.addAction(doneButton)
          present(alert, animated: true, completion: nil)
      }
      
  }
  
  extension MovieViewController: UITableViewDataSource {
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          presenter.rows
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          
          let cell = tableView.dequeueReusableCell(withIdentifier: MovieViewCell.identifier, for: indexPath) as! MovieViewCell
          
          let row = indexPath.row
          let title = presenter.getTitle(by: row)
          let overview = presenter.getOverview(by: row)
          let baseImageURL = presenter.getUrlImage(by: row)
          let data = presenter.getImageData(by: baseImageURL)
          cell.delegate = self
          cell.configureCell(title: title, overview: overview, data: data)
          return cell
          
      }
      
  
  extension MovieViewController : CellSubclassDelegate{
      func buttonTapped(cell: MovieViewCell) {
          guard (self.tableView.indexPath(for: cell) != nil) else {return}
              let customViewController = storyboard?.instantiateViewController(withIdentifier: &quot;MovieDeatilsViewController&quot;) as? MovieDeatilsViewController
          customViewController?.titlemovie = cell.movieTitle.text ?? &quot;&quot;
          customViewController?.imagemovie = cell.movieImage.image
          customViewController?.overview = cell.movieOverview.text ?? &quot;&quot;
          self.navigationController?.pushViewController(customViewController!, animated: true)
                      
                     
      }
  }
  extension MovieViewController :  CellDelegate{
      func AddFavourite(cell: FavouriteTableViewCell) {
          guard (self.tableView.indexPath(for: cell) != nil) else {return}
          
          movieTitle = cell.FavouritemovieTitle.text ?? &quot;&quot;
          movieOverview = cell.FavouritemovieOverview.text ?? &quot;&quot;
          movieImage =  cell.FavouriteImage.image
      }
      
      
      
  }
  </code></pre>
  <p>Here screenshot of the app desing.
  <a href="https://i.stack.imgur.com/1tIug.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1tIug.png" alt="app design " /></a></p>
  <p>Here is the screenshot of the application .</p>
  <p><a href="https://i.stack.imgur.com/aqgAl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aqgAl.png" alt="enter image description here" /></a></p>