xcode - 具有多个标识符的 prepareForSegue

xcode - prepareForSegue with multiple identifiers

我有一个包含两个部分的 tableView(不是静态单元格) 一个酒吧部分和一个俱乐部部分(每个部分都有几个单元格)。我希望同一部分的每个单元格都指向相同的 viewcontroller。

我只能访问第一个,不能访问最后一个。即使是第二部分的 de cells 也转到第一部分 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
    }
}

继续:

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

    }

每个原型单元格只能转到一个 viewController。如果您有 2 个不同的目的地 viewController,则需要 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
    }
}