-1

I have three arrays. One displays headers for each section on the tableView, one displays titles, and one has the links for each cell. When I run this code, I get:

Cannot subscript a value of type '[Array]' with an index of type 'IndexPath'

This error appears on this line:

viewController.websiteURL = links[myIndex]

My code for the firstViewController is:

import UIKit

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var headers = ["Bein Sports", "Sky Sports", "Alkass", "Other"]

    var channels = [["Bein Sports 1","Bein Sports 2","Bein Sports 3","Bein Sports 4","Bein Sports 5","Bein Sports 6","Bein Sports 7","Bein Sports 8","Bein Sports 9","Bein Sports 10","Bein Sports News"],
                              ["Sky Sports 1","Sky Sports 2","Sky Sports 3","Sky Sports 4","Sky Sports 5"],
                              ["Alkass One", "Alkass Two", "Alkass Three", "Alkass Four", "Alkass Five"],
                              ["BT Sports 1", "BT Sports 2", "Real Madrid TV", "Real Madrid TV 2"]]

    var links = [["https://www.google.ca","https://www.facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"],
                 ["https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"],
                 ["https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com","https://facebook.com"],
                 ["http://twitter.com","http://facebook.com","http://www.google.com","http://www.instagram.com"]]

    var myIndex : IndexPath = IndexPath()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return channels[section].count
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return channels.count //Rows
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return headers[section] //Sections
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = channels[indexPath.section][indexPath.row] //TextLabel
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        myIndex = NSIndexPath(row: indexPath.section, section: indexPath.row) as IndexPath
        performSegue(withIdentifier: "segue", sender: self)
    }

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {
        if (segue.identifier == "segue") {
            let viewController = segue.destination as! ChannelViewController
            // Now you have a pointer to the child view controller.
            // You can save the reference to it, or pass data to it.
            viewController.websiteURL = links[myIndex]

        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Karrar Al-Mimar
  • 4,199
  • 3
  • 12
  • 15

1 Answers1

2

You need to access the links array just like you do the channels array.

viewController.websiteURL = links[myIndex.section][myIndex.row]

Unrelated to your issue, but don't use NSIndexPath. Change:

myIndex = NSIndexPath(row: indexPath.section, section: indexPath.row) as IndexPath

to:

myIndex = IndexPath(row: indexPath.section, section: indexPath.row)
rmaddy
  • 314,917
  • 42
  • 532
  • 579