Swift-URL数组不起作用 [英] Swift - Array of URLs not working
问题描述
我有三个保存信息的数组。一个显示节标题,一个显示每个单元格的标题,一个提供到另一个viewController的链接。我正在使用Swift btw。
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.
但是当我运行它时,所有单元格都使用数组中的第一个url,而不使用其余的url。
But when I run it, all the cells use the first url in the array and not the rest of the url's.
这是我的代码:
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)
}
}
推荐答案
似乎您没有在代码中的任何地方使用链接数组。我没有检查此代码,希望它可以工作
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.
首先添加变量以在ChannelController中获取选定的单元格链接网址。
First add a variable to get selected cell link url in ChannelController.
let selcetedLink:String = ""
在didSelectRowAt方法中执行segue时,会从链接传递相应的url。为此,您需要覆盖prepareForSegue在FirstViewController中添加以下代码
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]
}
}
最后使用selectedLink值加载URL请求
Finally use the selectedLink value to load URL request
let url:URL = URL(string: selectedLink )!
这篇关于Swift-URL数组不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!