Swift - URL 数组不起作用
Posted
技术标签:
【中文标题】Swift - URL 数组不起作用【英文标题】:Swift - Array of URLs not working 【发布时间】:2017-06-23 03:11:12 【问题描述】:我有三个不同的数组来保存信息。一个显示部分标题,一个显示每个单元格的标题,一个提供到另一个 viewController 的链接。顺便说一句,我正在使用 Swift。
但是当我运行它时,所有单元格都使用数组中的第一个 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] 根据调试器,这行似乎有问题(显示为绿色)。 @KarrarAl-Mimar 将您的声明更改为var myIndex: IndexPath?
并将其设置在那里myIndex = indexPath
【参考方案1】:
myIndex = [indexPath.section][indexPath.row]
缺少您要下标的集合。您的意思可能是:
myIndex = channels[indexPath.section][indexPath.row]
【讨论】:
不要“尝试”。理解它,因为您的代码显然不是您打算编写的。 我是 swift 的初学者,正在学习互联网资源。我意识到,它与该行上的某些内容有关,因为当我删除您提到的行上的“indexPath.section”时,它运行时没有错误,但所有单元格都转到同一个第一个链接。你能告诉我另一种可能性吗,因为我不知道如何添加缺失的部分。【参考方案2】:您似乎没有在代码中的任何地方使用链接数组。我没有检查此代码希望它能工作
let url:URL = URL(string: "http://google.ca")!
因此,在单击您在单元格中提供的所有链接时,上述网址会静态加载。
首先在ChannelController中添加一个变量来获取选中的单元格链接url。
let selcetedLink:String = ""
在 didSelectRowAt 方法中执行 segue 时,从链接传递相应的 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 )!
【讨论】:
【参考方案3】:尝试可能的解决方案。
您需要定义两者相同,如下所示。
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"]]
获取如下网址
linkurl = links[indexPath.section][indexPath.row] as String
【讨论】:
以上是关于Swift - URL 数组不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Swift4 urlSession Decodable 中的 POST 方法不起作用(这里出了啥问题?)