假设我有两个表控制器相互连接。第一个表格显示来自我的国籍阵列[美国,亚洲,意大利]的信息。如果用户选择美国单元格,我希望美国项目数组中的四个项目显示在第二个视图控制器中。反之亦然,亚洲和意大利阵列,更具体地说是第二桌控制器中的亚洲和意大利物品阵列。
当第二个表显示取决于第一个表控制器中的用户选择时,如何在第二个控制器中显示特定的数组/项?
如有必要,可以改写。
答案 0 :(得分:0)
TABLEONE:
class TableOne: UITableViewController {
var countries = ["American", "Asian", "Italian"]
var items = [["Mcdonalds", "BurgerKing", "Big Boy"], ["PFChangs", "China One Buffet"], ["Pizza House", "Pizza Hit", "Olive Garden", "Maggianos"]]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueone")
{
//problem here with "sender"
let cellIndexPath = self.tableView!.indexPathForCell(sender as UITableViewCell)
if let unwrappedCellindexPath = cellIndexPath
{
var nextVC = (segue.destinationViewController as TableTwo)
nextVC.items = items[unwrappedCellindexPath.row]
}
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countries.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let selected = countries[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("cellone") as TableOneCell
cell.tableonecelllabel.text = selected
return cell
}
}
表二:
class TableTwo: UITableViewController {
var items: [String]?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let items = items
{
return items.count
}
else
{
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let selected = items![indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("celltwo") as TableTwoCell
cell.tablecelltwolabel.text = selected
return cell
}
}
你应该正确修改celltwo的TABLEVIEWCELL标识符。
答案 1 :(得分:0)
class TableOne: UITableViewController {
var countries = ["American", "Asian", "Italian"]
var items = [["Mcdonalds", "BurgerKing", "Big Boy"], ["PFChangs", "China One Buffet"], ["Pizza House", "Pizza Hit", "Olive Garden", "Maggianos"]]
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "segueone")
{
//problem here with "sender"
let cellIndexPath = self.tableView!.indexPathForCell(sender as UITableViewCell)
if let unwrappedCellindexPath = cellIndexPath
{
var nextVC = (segue.destinationViewController as TableTwo)
nextVC.items = items[unwrappedCellindexPath.row]
}
}
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return countries.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let selected = countries[indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("cellone") as TableOneCell
cell.tableonecelllabel.text = selected
return cell
}
}
class TableTwo: UITableViewController {
var items: [String]?
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let items = items
{
return items.count
}
else
{
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let selected = items![indexPath.row]
var cell = tableView.dequeueReusableCellWithIdentifier("celltwo") as TableTwoCell
cell.tablecelltwolabel.text = selected
return cell
}
}
非常感谢Javier的代码和见解!