Swift – 一些url无效
我有三个不同的arrays来保存信息。 一个显示部分标题,一个显示每个单元格的标题,另一个提供另一个viewController的链接。 我正在使用Swift btw。
但是当我运行它时,所有单元格都使用数组中的第一个url,而不是url的其余部分。
这是我的代码:
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) } }
似乎你没有在你的代码中的任何地方使用链接数组。我没有检查这个代码,希望它能工作
let url:URL = URL(string: "http://google.ca")!
所以上面的url静态加载,同时点击您在单元格中提供的所有链接。
首先添加一个variables来获取ChannelController中选定的单元格链接url。
let selcetedLink:String = ""
在didSelectRowAt方法中执行segue时,会从link中传递相应的url。要做到这一点,您需要重写prepareForSegue,在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] } }
最后使用selectedLink值来加载URL请求
let url:URL = URL(string: selectedLink )!
myIndex = [indexPath.section][indexPath.row]
缺less你想要下标的集合。 你也许意思是:
myIndex = channels[indexPath.section][indexPath.row]
试试可能的解决
你需要定义如下。
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"]]
获取url如下
linkurl = links[indexPath.section][indexPath.row] as String