简体   繁体   中英

3 tableView Sections with custom Cell Segues swift

So I am trying to have 3 different segues with 3 different section cells in a tableview. I am getting the initial tableview to populate with the custom cell data, but having trouble setting each cell to segue to each corresponding new ViewController...So 3 cells with 3 different segues is what I want...appreciate the help!

class ProfileTableViewController: UITableViewController {

 var tableData: [String] = ["milan", "parisTower", "alps_1-blur", "alps_1", "meStanding"]


        //register TravelBook Custom Cell
        let nib = UINib(nibName: "TravelBookCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "TravelBookCell")
        self.tableView.dataSource = self

        }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 3
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(section == 0) {
            return 1
        }
        if(section == 1) {
            return 1 
        }
        else {
            return tableData.count
        }

    }




    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        _ = tableView.cellForRowAtIndexPath(indexPath)
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        performSegueWithIdentifier("TravelBookDetailSegue", sender: TravelBookTableViewCell())
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {



    }



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



        switch indexPath.section{

        // 1st custom cell
        case 0:
            let myProfile = tableView.dequeueReusableCellWithIdentifier("ProfileHeaderCell") as! VideoTableViewCell

            myProfile.frame.size = CGSize(width: 600.0, height: 60.0)


            return myProfile




        // 2nd custom cell
        case 1:
            let myInfo = tableView.dequeueReusableCellWithIdentifier("AlbumCell") as! AlbumTableViewCell



            return myInfo 




          // 3rd Custom Cell
          default:
            let cell = tableView.dequeueReusableCellWithIdentifier("TravelBookCell") as! TravelBookTableViewCell

            cell.travelBookImage.image = UIImage(named: tableData[indexPath.row])
            cell.travelBookTitle.text = tableData[indexPath.row]

            return cell

        }
    }

you can check indexPath.section in didSelectRowAtIndexPath: delegate method like,

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch indexPath.section{

        case 0:
             //first segue
             break
        case 1:
             //second segue
             break
        case 2:
             //third segue
             break
}

What you did is that when you click your cell the it will call didSelectRow method and there you have specified only one segue and no conditions to perform which segue. So you need to add if or switch condition as said by Akhilrajtr.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM