0

I have three different arrays that hold information. One displays the section title, one displays the title of each cell, and one provides a link to the other viewController. I am using Swift btw.

But when I run it, all the cells use the first url in the array and not the rest of the url's.

Here is my code:

import UIKit

class FirstViewController: UIViewController, UITableViewDataSource, 
UITableViewDelegate {
    var headers = ["Bein Sports", "Sky Sports", "Alkass"]
    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"]]
    var links = ["https://google.ca","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","https://facebook.com"]
    var myIndex: IndexPath?


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

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

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

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

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        myIndex = indexPath
        performSegue(withIdentifier: "segue", sender: self)
    }
}
Karrar Al-Mimar
  • 4,199
  • 3
  • 12
  • 15
  • Which line of code exactly is causing the error? – rmaddy Jun 23 '17 at 03:15
  • 1
    I'm sure someone will find what you're doing wrong, but this is like the easiest possible kind of issue to debug. You should take the opportunity to learn to use the debugger. Once your app crashes, read the contents of the stuff that causes it and you'll see the problem. If you cannot use the debugger to find the cause of this, you won't get too far doing any kind of app. – Fernando Mazzon Jun 23 '17 at 03:17
  • myIndex = [indexPath.section][indexPath.row] thats the line that seems to be faulty according to the debugger (it shows as green). – Karrar Al-Mimar Jun 23 '17 at 03:35
  • @KarrarAl-Mimar change your declaration to `var myIndex: IndexPath?` – Leo Dabus Jun 23 '17 at 03:35
  • and set it there `myIndex = indexPath` – Leo Dabus Jun 23 '17 at 03:35
  • @LeoDabus it didn't work. – Karrar Al-Mimar Jun 23 '17 at 03:38
  • it didn't work doesn't make any sense. if you want the indexPath thats what you need to do – Leo Dabus Jun 23 '17 at 03:39
  • update your question with your actual code and the error you got – Leo Dabus Jun 23 '17 at 03:40
  • @LeoDabus I updated the question. – Karrar Al-Mimar Jun 23 '17 at 03:57
  • The URL is hardcoded on the second screen. Your links array is not used at all. – Fernando Mazzon Jun 23 '17 at 04:00
  • @KarrarAl-Mimar links array didn't used anywhere in your code – Dharma Jun 23 '17 at 04:01
  • @KarrarAl-Mimar you might take a look at some other questions on how you should use `prepare(for segue: UIStoryboardSegue, sender: Any?)` method to pass your objects to a different view controller. note that you need to declare your object at the destination view controller https://stackoverflow.com/a/32284525/2303865 – Leo Dabus Jun 23 '17 at 04:10
  • @KarrarAl-Mimar Would you mind debug cellForRawAtIndexPath and in that method just check how many times that method is called and print links array's index value and index will be indexpath.row. You will easily find out issue by doing this. – Kirti Parghi Jun 23 '17 at 04:22

3 Answers3

1
myIndex = [indexPath.section][indexPath.row]

is missing the collection you want to subscript. You perhaps meant:

myIndex = channels[indexPath.section][indexPath.row]
Fernando Mazzon
  • 3,562
  • 1
  • 19
  • 21
  • 1
    Don't "try it". Understand it, as your code is obviously not what you intended to write. – Fernando Mazzon Jun 23 '17 at 03:22
  • I am beginner to swift, learning off internet sources. I realized, it has to do with something on that line because when I remove the 'indexPath.section' on the line you referred to, it runs without an error but all the cells go to the same first link. Could you tell me another possibility because I don't know how to add the missing part. – Karrar Al-Mimar Jun 23 '17 at 03:31
0

Seems you didn't used links array anywhere in your code.I didn't check this code hope it will work

let url:URL = URL(string: "http://google.ca")!

So the above url loaded statically while clicking all links which you provide in cell.

First add a variable to get selected cell link url in ChannelController.

let selcetedLink:String = ""

While performing segue in didSelectRowAt method pass corresponding url from link.To do that you need to override prepareForSegue add below code in your FirstViewController

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
   if segue.identifier == "segue" { // check your segue identifier
      let theDestination = (segue.destinationViewController as ChannelController)
      theDestination.selcetedLink = self.links[myIndex.row]
    }
} 

Finally use the selectedLink value to load URL request

let url:URL = URL(string: selectedLink )!
Dharma
  • 3,007
  • 3
  • 23
  • 38
0

Try possible solution.

You need to define both same as below .

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

var links = [["https://google.ca","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","https://facebook.com"]]

Get url as below

linkurl = links[indexPath.section][indexPath.row] as String
KKRocks
  • 8,222
  • 1
  • 18
  • 84