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)
    }
}
13
  • Which line of code exactly is causing the error? Commented Jun 23, 2017 at 3: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. Commented Jun 23, 2017 at 3:17
  • myIndex = [indexPath.section][indexPath.row] thats the line that seems to be faulty according to the debugger (it shows as green). Commented Jun 23, 2017 at 3:35
  • @KarrarAl-Mimar change your declaration to var myIndex: IndexPath? Commented Jun 23, 2017 at 3:35
  • and set it there myIndex = indexPath Commented Jun 23, 2017 at 3:35

3 Answers 3

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

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

myIndex = channels[indexPath.section][indexPath.row]
Sign up to request clarification or add additional context in comments.

2 Comments

Don't "try it". Understand it, as your code is obviously not what you intended to write.
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.
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 )!

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.