Swift- URL数组无法正常工作



我有三个不同的阵列可以容纳信息。一个显示部分标题,一个显示每个单元格的标题,并且一个提供了指向另一个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)
    }
}
myIndex = [indexPath.section][indexPath.row]

缺少您要下标的集合。你的意思是:

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

似乎您没有在代码中的任何地方使用链接数组。

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

因此,上面的URL在单击您在单元格中提供的所有链接时静态加载。

首先添加一个变量以获取ChannelController中的选定单元格url。

let selcetedLink:String = ""

在didSelectrowat方法中执行segue时,从链接中通过对应的URL。

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 )!

尝试可能的解决方案。

您需要定义与下面相同的。

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

最新更新