gpt4 book ai didi

xcode - 具有多个标识符的 prepareForSegue

转载 作者:行者123 更新时间:2023-11-28 08:47:23 25 4
gpt4 key购买 nike

我有一个包含两个部分的 tableView(不是静态单元格)一个酒吧部分和一个俱乐部部分(每个部分都有几个单元格)。我希望来自同一部分的每个单元格都进入同一个 View Controller 。

我只能访问第一个,不能访问最后一个。即使是来自第二部分的 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

}

最佳答案

每个原型(prototype)单元只能连接到一个 viewController。如果您有 2 个不同的目标 View Controller ,则需要 2 个原型(prototype)单元格: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)

然后您可以将每个原型(prototype)单元连接到相应的 viewController。给这两个 segue 中的每一个一个唯一的标识符,例如 "barSegue""clubSegue",然后您可以在 prepareForSegue 中使用它们来配置目标 View Controller 。

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
}
}

关于xcode - 具有多个标识符的 prepareForSegue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34969336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com