0

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:

fatal error: Index out of range

Here is what happens after it runs

my code 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 = 0

    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 = [indexPath.section][indexPath.row]
        performSegue(withIdentifier: "segue", sender: self)
    }
}

in the debugger this is the highlighted line:

myIndex = [indexPath.section][indexPath.row]
Karrar Al-Mimar
  • 4,199
  • 3
  • 12
  • 15
  • 1
    What is the value of `IndexPath` (section, row)? – l'L'l Jun 24 '17 at 00:52
  • This is the same problem [in your last question](https://stackoverflow.com/questions/44712469/swift-array-of-urls-not-working). Did you review the answers there and the changes you made? – rmaddy Jun 24 '17 at 00:53
  • @rmaddy this is the correct question. – Karrar Al-Mimar Jun 24 '17 at 00:54
  • But the line causing the issue is the same line you asked about [in your previous question](https://stackoverflow.com/questions/44712469/swift-array-of-urls-not-working). You were given answers about fixing that line of code. So why are you posting a new question with the same bad line of code? – rmaddy Jun 24 '17 at 00:54
  • @rmaddy I had errors in the code that were already corrected and answers were trying to solve both. I attempted to do what the answers said but it was all messed up on my end and I didn't know what is what. – Karrar Al-Mimar Jun 24 '17 at 00:59
  • The code `myIndex = [indexPath.section][indexPath.row]` shouldn't even compile, let alone run. Is that actually your "real" code? – rmaddy Jun 24 '17 at 01:00
  • Yes, I'm a brand new coder, learning off internet. The code used to be: myIndex = indexPath.row but it used to refer only to the first link in the "links" array and I wouldn't get an error. So I added the "indexPath.section" thinking it would solve it. – Karrar Al-Mimar Jun 24 '17 at 01:04
  • @rmaddy can you help me? It would be very much appreciated. – Karrar Al-Mimar Jun 24 '17 at 01:14
  • If you want help you need to post real code that is causing your issue. The code in your question won't even compile so it can't be causing the error you claim. – rmaddy Jun 24 '17 at 01:16
  • @rmaddy I've posted an image showing exactly what happens. – Karrar Al-Mimar Jun 24 '17 at 01:20
  • It's not valid code. I'm amazed it compiles. The real question is why is `myIndex` an `Int` when you need to store the whole `IndexPath`? – rmaddy Jun 24 '17 at 01:26
  • @rmaddy what should I change? – Karrar Al-Mimar Jun 24 '17 at 01:31
  • @rmaddy it compiles because it says make an array containing `indexPath.section` and then access the `indexPath.row`th element. Which if row is anything other than 0 is going to be a bound violation. It is valid but totally incorrect code. – Paulw11 Jun 24 '17 at 01:38
  • @KarrarAl-Mimar what are you actually trying to achieve with that `myIndex...` line? – Paulw11 Jun 24 '17 at 01:42
  • @Paulw11 I have a table and I want it so that when I click each cell it takes me to the same screen that has WKWebView which will be filled from the array "links". I learned online that I need to declare "myIndex = 0" so that the arrays are accessible by order, starting from 0. – Karrar Al-Mimar Jun 24 '17 at 02:02
  • In `prepareForSegue` you can use `tableview.indexPathForSelectedRow`. The code you are then looking for to get the url is `links[indexPath.section][indexPath.row]` – Paulw11 Jun 24 '17 at 02:04
  • @Paulw11 Of course. That makes sense (about why the code compiles). – rmaddy Jun 24 '17 at 02:11
  • @Paulw11 Is that the exact code I add? Can you write it out exactly because I attempted to do as you said but I don't think I wrote it correctly. Thnx man. – Karrar Al-Mimar Jun 24 '17 at 03:16

1 Answers1

0

you can store myIndex as IndexPath and using myIndex to get indexPath row and section just like this

     var myIndex : IndexPath = IndexPath()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

      myIndex = NSIndexPath(row: indexPath.section, section: indexPath.row) as IndexPath
      performSegue(withIdentifier: "segue", sender: self)
 }
Pratik Lad
  • 464
  • 4
  • 9