无法使用 [NSIndexPath] 类型的索引下标 [String] 类型的值 - prepareForSegue
Cannot subscript a value of type [String] with an index of type [NSIndexPath] - prepareForSegue
我正在尝试使用 prepareForSegue 函数将多行从 UITableView 发送到另一个 UITableView。
当我只向第二个 UITableView 发送一个选项时,该应用程序运行良好,但是当我选择多行并将它们作为 header 发送到第二个 UITableView 时(例如,如果我单击首先在 "a" 行,然后是 "d" 行,然后是 "c" 行...第二个 UITableView 只显示 "c" 行作为 header,而不是其他 2 行),它给出了一个错误。
错误图片:
这些是我为第一个 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.
initialTableView.allowsMultipleSelection = true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueA" {
if let destination = segue.destinationViewController as? Content {
if let selectedRows = initialTableView.indexPathsForSelectedRows {
destination.title = array[selectedRows]
}
}
}
}
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
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let cell = tableView.cellForRowAtIndexPath(indexPath)
if (cell?.accessoryType == UITableViewCellAccessoryType.Checkmark) {
cell!.accessoryType = UITableViewCellAccessoryType.None
} else {
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
}
}
第二个视图包含这些行:
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]
}
所以,如果我想选择多个行并将它们从第一个 UITableView 发送到第二个 UITableView...我该如何解决这个错误?
谢谢。
您的问题是 selectedRows
是 NSIndexPath
的数组。你不能用它来索引你的数组。您需要将 String
的数组传递给第二个视图控制器,而不是只设置一个字符串。
使用map
到select来自array
的字符串传递给目的地viewController的contentArray
属性:
destination.contentArray = selectedRows.map { array[[=10=].row] }
您需要为 destination.title
确定一个新的合理设置。
您需要从 viewDidLoad
中删除此行:
contentArray.append(title!)
我正在尝试使用 prepareForSegue 函数将多行从 UITableView 发送到另一个 UITableView。
当我只向第二个 UITableView 发送一个选项时,该应用程序运行良好,但是当我选择多行并将它们作为 header 发送到第二个 UITableView 时(例如,如果我单击首先在 "a" 行,然后是 "d" 行,然后是 "c" 行...第二个 UITableView 只显示 "c" 行作为 header,而不是其他 2 行),它给出了一个错误。
错误图片:
这些是我为第一个 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.
initialTableView.allowsMultipleSelection = true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueA" {
if let destination = segue.destinationViewController as? Content {
if let selectedRows = initialTableView.indexPathsForSelectedRows {
destination.title = array[selectedRows]
}
}
}
}
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
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let cell = tableView.cellForRowAtIndexPath(indexPath)
if (cell?.accessoryType == UITableViewCellAccessoryType.Checkmark) {
cell!.accessoryType = UITableViewCellAccessoryType.None
} else {
cell!.accessoryType = UITableViewCellAccessoryType.Checkmark
}
}
第二个视图包含这些行:
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]
}
所以,如果我想选择多个行并将它们从第一个 UITableView 发送到第二个 UITableView...我该如何解决这个错误?
谢谢。
您的问题是 selectedRows
是 NSIndexPath
的数组。你不能用它来索引你的数组。您需要将 String
的数组传递给第二个视图控制器,而不是只设置一个字符串。
使用map
到select来自array
的字符串传递给目的地viewController的contentArray
属性:
destination.contentArray = selectedRows.map { array[[=10=].row] }
您需要为 destination.title
确定一个新的合理设置。
您需要从 viewDidLoad
中删除此行:
contentArray.append(title!)