0

我正在使用 swift 在 xCode 10.2 中工作。我在我的第一个视图控制器中为 6 个表视图创建了全局变量。在情节提要中,我有 6 tableViewControllers。在一个单独的 swift 文件中,我创建了一个表结构来保存一个数组并显示每个相应单元格中的数据。在每个视图控制器中didSelectRowAt连接下一个表视图。我的问题是当我到达最后一个表格视图时。我需要将网站 URL 关联到第五个表上的数组。我不断收到一条错误消息,指出无法将字符串转换为 URL。请帮忙!

 var fifthArray = [
    FifthTableStruct(FifthTitle: ["Energy Guide", "https://www.google.com", "Warranty Page", "Use & Care Guide", "Specification Sheet", "FIT System", "Installation Instructions"]),
    FifthTableStruct(FifthTitle: ["Energy Guide", "Warranty Page", "Use & Care Guide", "Specification Sheet", "FIT System", "Installation Instructions"])
    ]

var sixthArray = [
    SixthTableStruct(SixthTitle:  ["https://www.whirlpool.com/content/dam/global/documents/201708/EnergyGuide-W11037203-RevA.pdf", "https://www.whirlpool.com/content/dam/global/documents/201708/WarrantyPage-W11037201-W.pdf", "https://www.whirlpool.com/content/dam/global/documents/201708/UseandCareGuide-W11037201-RevA.pdf", "https://www.whirlpool.com/content/dam/global/documents/201711/WL170160A_p2.pdf", "https://www.whirlpool.com/content/dam/global/documents/201901/wash-performance-guarantee-en.pdf", "https://www.whirlpool.com/content/dam/global/documents/201711/InstallationInstructions-W10682737-RevA.pdf"])
    ]

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let urlString = self.sixthArray[indexPath.row]
    if let url = URL(fileURLWithPath: urlString)
    {
        UIApplication.shared.openURL(url)
    }

}

我在与 viewController 分开的数组文件中有 tableStruct 的代码。

    import Foundation
    import UIKit


    struct SecondTableStruct {
        var SecondTitle = [String]()
    }
    struct ThirdTableStruct {
        var ThirdTitle = [String]()
    }
   struct FourthTableStruct {
        var FourthTitle = [String]()
    }
    struct FifthTableStruct {
        var FifthTitle = [String]()
    }
    struct SixthTableStruct {
        var SixthTitle = [String]()
    }
4

2 回答 2

0

sixthArraySixthTableStructs 的数组,aSixthTableStruct有一个字段SixthTitle,其类型是 的数组String

因此,要获取存储在其中的单个字符串,sixthArray您需要:

  1. 索引到sixthArray获取类型的单个值SixthTable,我们称之为intermediate1
  2. 选择SixthTitle字段intermediate1来获取类型数组的值String,我们称之为intermediate2
  3. 索引intermediate2以获得单个String

在代码中:

let intermediate1 = sixthArray[someIndex]
let intermediate2 = intermediate1.SixthTitle
let urlString = intermediate2[someOtherIndex]

我们无法告诉您您需要的两个索引值是什么,一个大概是indexPath.row. (如果您愿意,当然可以将上述三行写成不带中间体的一行​​。)

一些建议,首先你似乎有页面标题和相关的 URL,它们形成了一对紧密相连的数据值,被分解并存储在单独的数组中,需要你仔细管理这些数组中项目的顺序并失去紧密的关联项之间。考虑一个由一些 组成的数组struct,比如PageDetails,具有适当的属性,比如titleURL,以将它们保持在一起。

其次,数组可以保存URLs,而不仅仅是Strings...

高温高压

于 2019-04-19T05:09:26.400 回答
-1

在您的 didSelectRowAt 中执行以下操作,目前您正在直接访问 struct 但不是其具有名称的数组SixthTitle

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let urlString = self.sixthArray.SixthTitle[indexPath.row]
if let url = URL(fileURLWithPath: urlString)
{
    UIApplication.shared.openURL(url)
}

}

于 2019-04-19T06:43:40.633 回答