【发布时间】:2015-12-19 15:57:38
【问题描述】:
我在一个包含 5 行(“a”到“e”)的 UITableView 中工作,当我单击其中一行时,它会在下一个 UITableView 中显示我选择作为标题的字母(带有prepareForSegue 函数)。实际上,直到这一点它完美地工作..但是当我在第一个 UITableView 中单击多行后尝试在第二个 tableView 中添加更多“标题”时,它只会显示最后一个选项(例如,如果我单击首先是“a”,然后是“d”,然后是“c”……第二个 UITableView 只显示行“c”作为标题,而不是其他 2 行)。
如何选择多行并通过 prepareForSegue 函数将这些选项发送到第二个 UITableView?
第一个视图的代码行如下:
let array = ["a", "b", "c", "d", "e"]
@IBOutlet var initialTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueA" {
if let destination = segue.destinationViewController as? Content {
if let selectedRow = initialTableView.indexPathForSelectedRow?.row {
destination.title = array[selectedRow]
}
}
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellA", forIndexPath: indexPath) as UITableViewCell
cell.textLabel!.text = array[indexPath.row]
return cell
}
第二个视图包含以下几行:
var contentArray:[String] = []
@IBOutlet var contentTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
contentArray.append(title!)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return contentArray.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellContent = tableView.dequeueReusableCellWithIdentifier("cellContent", forIndexPath: indexPath) as UITableViewCell
cellContent.textLabel!.text = "Second test"
return cellContent
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return contentArray[section]
}
谢谢!
【问题讨论】:
标签: ios swift uitableview segue