Search code examples
iosswiftuitableviewsegueswift-protocols

Segue from TableView into another Tableview using gesture


I have FirstTableView (TableView) which is populated by cells that have an image and label. If users tap the label, I want to segue into another tableview (SecondTableView), which will use the label from the previous tableview as a path(foldername) to search for images within that folder and display them as cells.

I have bits and pieces of the code written together, but I'm not quite sure where to pass the perform segue and prepare for segue functions.

Here is the extension of my FirstTableViewController, which populate the cells of the FirstTableView:

FirstTableView

extension FirstTableViewController: UITableViewDelegate, UITableViewDataSource{
   
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataCells.count
    }
        
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let folderCell = dataCells[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "FirstTableCell") as! FirstTableViewCell
        cell.setCell(cell:folderCell)
        
        return cell
        
        }
}

Here is the code for the FirstTAbleViewCell. Notice that I've created the UITapGestureRecognizer constant and a protocol. If the FolderLabel is clicked, I want to segue into the next tableview.

func setCell is just the function to populate the FirstTableViews.

FirstTableViewCells

import UIKit

protocol LabelCellDelegate {
    func func1(sender:Any)
}

    
    
    class FirstTableViewCell: UITableViewCell {
    
    
        @IBOutlet weak var ImageView: UIImageView!
        @IBOutlet weak var FolderLabel: UILabel!
        
        var delegate: LabelCellDelegate?
        override func awakeFromNib() {
            super.awakeFromNib()
            
            FolderLabel.isUserInteractionEnabled = true
            let recognizer = UITapGestureRecognizer(target: self, action:Selector(("handleTap:")))
            recognizer.numberOfTapsRequired = 1
            self.FolderLabel.addGestureRecognizer(recognizer)
        }
        
        func handleTap(recognizer: UITapGestureRecognizer){
            delegate!.func1(sender: self)
        }
        func setCell (cell : FolderCell){
            self.ImageView.image = cell.image
            self.FolderLabel.text = cell.label
        }
    }

Next is the SecondTableViewController. Somehow, after the tap of the cell from the FirstTableView I want to get the label name that was tapped, and use that name as a directory to search. Grab all the images within the directory and display them as images in cells in SecondTableView. I'm not quite sure where I should be calling the gesture function and which TableViewController should be the delegate of the cell protocol.

SecondTableView

import UIKit

class SecondTableViewController: UIViewController {
    var Imagefiles: [String] //Imagefiles String. This are all images from tapped folder label. 
    var CellObjects: [folderCell] // Cellobjects to fill the cells. contains images and image names.
    
     // need to get folder name from FirstTableViewCell how?
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        imageFiles = getImageObjects(folder: Foldercell) //how to get access to folderCell???????
        cellObjects = createArray(imageFiles: imageFiles)
        }
    
    func getImageObjects(folder: FolderCell) -> imageFiles: String {
        //FUNCTION To search tapped label and grab all image file paths. 
    
    
    func createArray(imageFiles: string)-> tempArray: [folderCell]{
         //We use this function to take the image files from above function and 
         //create cell objects to populate our cells 
       //in SecondTableView

        
    
   
        }
      }
    
    
    
}
    
}



  extension ViewController: LabelCellDelegate{
        func1(folderLabel : String) {
        return folderLabel
        
        
        }
    }

extension LablesTableView:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return CellObjects.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellFolderObject = folderObjects[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "SecondCell") as! LabelCell
         let folderCell = CellObjects[indexPath.row]

        cell.setCell(cell: folderCell)
     
        return cell

As you can see, I haven't called perform segue and prepare for segue. I'm not sure where I should be calling them. I'm also unsure about where the delegate to the gesture protocol should be.

Thank you.


Solution

  • Inside cellForRowAt set

    cell.delegate = self
    

    //

    then inside this delegate function of the VC ( conform to the protocol LabelCellDelegate )

    extension FirstTableViewController:LabelCellDelegate {
       func func1(sender: FirstTableViewCell) {
          self.performSegue(withIdentifier: "goToSecond", sender:sender.FolderLabel.text!)
      }
    }
    

    //

    the VC method is triggered from here

    func handleTap(recognizer: UITapGestureRecognizer){
      delegate!.func1(sender: self)
    }
    

    //

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let secondController = segue.destination as? SecondTableViewController {
         secondController.sendedStr = sender as! String
      }
    }