简体   繁体   English

3个带有自定义单元格Segues的tableView节

[英]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. 因此,我试图在Tableview中使用3个不同的单元格来设置3个不同的Segue。 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! 我正在使用自定义单元格数据填充初始表格视图,但是在将每个单元格设置为分别连接到每个相应的新ViewController时遇到了麻烦。因此,我想要具有3种不同顺序的3个单元格...感谢帮助!

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, 您可以在didSelectRowAtIndexPath:检查indexPath.section didSelectRowAtIndexPath:类似的委托方法,

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. 您所做的是,当您单击单元格时,它将调用didSelectRow方法,并且您仅指定了一个序列,而没有条件执行该序列。 So you need to add if or switch condition as said by Akhilrajtr. 因此,您需要按Akhilrajtr所述添加if或switch条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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