xcode - 具有多个标识符的prepareForSegue

时间:2016-01-23 21:53:31

标签: xcode swift segue identifier

我有一个包含两个部分的tableView(非静态单元格) 酒吧部分和俱乐部部分(每个都有几个单元格)。我希望同一部分的每个单元格都转到同一个viewcontroller。

我只能访问第一个,而不是最后一个。甚至第二部分中的de单元也会转到第一个viewcontroller。

有人能看出我的错误吗?

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 2
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    if section == 0 {
    return areas.bars.count
    } else {

    return areas.clubs.count
}
}


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

    let cell = tableView.dequeueReusableCellWithIdentifier("mainIdentifier", forIndexPath: indexPath)
    if  indexPath.section == 0 {
        let bars = areas.bars
        let bar = bars[indexPath.row]

        cell.textLabel?.text = bar.name

        return cell
    } else {
        let clubs = areas.clubs
        let club = clubs[indexPath.row]

        cell.textLabel?.text = club.name

        return cell
    }
}

segue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "barsandclubsIdentifier"{
        let selectedIndex = self.tableView.indexPathForSelectedRow!
        let selectedBar = self.areas.bars[selectedIndex.row]
       let detailBarsAndClubsViewController = segue.destinationViewController as! DetailBarOrClubViewController
    detailBarsAndClubsViewController.bars = selectedBar
    }
     else {
        let selectedIndex = self.tableView.indexPathForSelectedRow!
        let selectedClub = self.areas.clubs[selectedIndex.row]
        let detailBarsAndClubsTwoViewController = segue.destinationViewController as! DetailBarOrClubTwoViewController
        detailBarsAndClubsTwoViewController.clubs = selectedClub

    }

1 个答案:

答案 0 :(得分:0)

每个原型单元只能转换为单个viewController。如果您有2个不同的目标viewControllers,则需要2个原型单元格:1个用于条形图,1个用于条形图。为每个人提供唯一标识符,例如"barCell""clubCell"

然后在cellForRowAtIndexPath中,将正确的单元格出列:

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

    let cellID = ["barCell", "clubCell"][indexPath.section]

    let cell = tableView.dequeueReusableCellWithIdentifier(cellID, forIndexPath: indexPath)

然后,您可以连接每个原型单元格以转到相应的viewController。为这两个segue中的每一个分配一个唯一标识符,例如"barSegue""clubSegue",然后您可以使用prepareForSegue中的那些来配置目标viewController。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "barSegue"{
        let selectedIndex = self.tableView.indexPathForSelectedRow!
        let selectedBar = self.areas.bars[selectedIndex.row]
        let detailBarViewController = segue.destinationViewController as! DetailBarViewController
        detailBarsViewController.bars = selectedBar
    }
    else if segue.identifier = "clubSegue" {
        let selectedIndex = self.tableView.indexPathForSelectedRow!
        let selectedClub = self.areas.clubs[selectedIndex.row]
        let detailClubViewController = segue.destinationViewController as! DetailClubViewController
        detailClubsViewController.clubs = selectedClub
    }
}