Search code examples
arraysswiftuitableviewnsindexpath

Cannot subscript a value of type [String] with an index of type [NSIndexPath] - prepareForSegue


I'm trying to send multiple rows from a UITableView to another UITableView using prepareForSegue function.

When I just send one option to the 2nd UITableView, the app works perfectly, BUT when I'm choose multiple rows and send those to the second UITableView as a header (for example, if I click first in "a" row, then "d" row and then "c" row... the second UITableView just show me the row "c" as a header, not the other 2 rows), it gives an error.

Pic of the error:

enter image description here

These are the lines I wrote for the first 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
    }
}

The second view contains these lines:

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

So, if I wanna choose multiples rows and send these from the first to the second UITableView... how can I solve this error?

Thanks.


Solution

  • Your problem is that selectedRows is an array of NSIndexPaths. You can't use that to index into your array. You need to pass an array of Strings to your second view controller instead of just setting a single string.

    Use map to select the strings from array to pass to the contentArray property of the destination viewController:

    destination.contentArray = selectedRows.map { array[$0.row] }
    

    You'll need to decide a new sensible setting for destination.title.

    You'll want to remove this line from viewDidLoad:

    contentArray.append(title!)