Reputation: 6459
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)
}
}
Upvotes: 0
Views: 1158
Reputation: 8322
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
Upvotes: 0
Reputation: 3013
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 )!
Upvotes: 0
Reputation: 3591
myIndex = [indexPath.section][indexPath.row]
is missing the collection you want to subscript. You perhaps meant:
myIndex = channels[indexPath.section][indexPath.row]
Upvotes: 1