2

I have a navigation bar with a table view controller that display 4 images (4 cells). I have 4 viewControllers that I need to show when the user clicks on the cell ( each image show a specific VC ) ??

any ideas would be much appreciated

here is my code :

class MenuTable: UITableViewController {

    @IBOutlet weak var imgtable: UITableView!

    var menuImage: [UIImage] = [
        UIImage(named: "image1")!,
        UIImage(named: "image2")!,
        UIImage(named: "image3")!,
        UIImage(named: "image4")!
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        imgtable.dataSource = self
        imgtable.delegate = self
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return menuImage.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("menucell") as! Menucell
        cell!.imageview.image = menuImage[indexPath.row]

        return cell
    }

}
maxwell
  • 3,788
  • 6
  • 26
  • 40
Mohammed Riyadh
  • 883
  • 3
  • 11
  • 34
  • 3
    did you even tried to google it ? – Umair Afzal Jul 25 '16 at 12:22
  • give each button a tag. so they will have tags 0, 1, 2, 3. now let them all trigger the same method on click. on click check what is the tag of the button, if it is 0, open vc 0. do you know how to initiateand display a view controller? – Mohamad Bachir Sidani Jul 25 '16 at 12:25
  • With only four possibilities, learning about static cells should be an option: https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2 – Phillip Mills Jul 25 '16 at 12:32

1 Answers1

1

You need to implement didSelectRowAtIndexPath delegate method of UITableView like this

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var viewController: UIViewController = UIViewController()
    switch (indexPath.row) {
        case 0:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController1") as! viewController1
        case 1:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController2") as! viewController2
        case 2:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController3") as! viewController3
        case 3:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController4") as! viewController4
        default:
            print("default")
    }
    self.navigationController?.pushViewController(viewController, animated: true)
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183