Swift - 无法使用“IndexPath”类型的索引为“[Array<String>]”类型的值下标

Posted

技术标签:

【中文标题】Swift - 无法使用“IndexPath”类型的索引为“[Array<String>]”类型的值下标【英文标题】:Swift - Cannot subscript a value of type '[Array<String>]' with an index of type 'IndexPath' 【发布时间】:2017-06-27 20:39:11 【问题描述】:

我有三个数组。一个显示 tableView 上每个部分的标题,一个显示标题,一个显示每个单元格的链接。当我运行这段代码时,我得到:

不能用“IndexPath”类型的索引为“[Array]”类型的值下标

此错误出现在这一行:

viewController.websiteURL = links[myIndex]

我的 firstViewController 代码是:

import UIKit

class FirstViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 
    var headers = ["Bein Sports", "Sky Sports", "Alkass", "Other"]

    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"],
                              ["BT Sports 1", "BT Sports 2", "Real Madrid TV", "Real Madrid TV 2"]]

    var links = [["https://www.google.ca","https://www.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"],
                 ["http://twitter.com","http://facebook.com","http://www.google.com","http://www.instagram.com"]]

    var myIndex : IndexPath = IndexPath()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return channels[section].count
    

    func numberOfSections(in tableView: UITableView) -> Int 
        return channels.count //Rows
    

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? 
        return headers[section] //Sections
    

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = channels[indexPath.section][indexPath.row] //TextLabel
        return cell
    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
        myIndex = NSIndexPath(row: indexPath.section, section: indexPath.row) as IndexPath
        performSegue(withIdentifier: "segue", sender: self)
    

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    
        if (segue.identifier == "segue") 
            let viewController = segue.destination as! ChannelViewController
            // Now you have a pointer to the child view controller.
            // You can save the reference to it, or pass data to it.
            viewController.websiteURL = links[myIndex]

        
    

【问题讨论】:

它在寻找一个数字,而不是NSIndexPath。试试myIndex.row。另请阅读如何在询问时提供MCVE。 @brandonscript 它给了我>'inout IndexPath' 不能转换为 'IndexPath' 【参考方案1】:

您需要像访问channels 数组一样访问links 数组。

viewController.websiteURL = links[myIndex.section][myIndex.row]

与您的问题无关,但不要使用NSIndexPath。变化:

myIndex = NSIndexPath(row: indexPath.section, section: indexPath.row) as IndexPath

到:

myIndex = IndexPath(row: indexPath.section, section: indexPath.row)

【讨论】:

你明白为什么吗? 是的,需要根据哪一行和哪一段知道。

以上是关于Swift - 无法使用“IndexPath”类型的索引为“[Array<String>]”类型的值下标的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 中的“didSelectRowAt indexPath”问题

Swift:使用 didSelectItemAt indexPath 函数仅选择单元格,即使我点击其中的 UI 元素也是如此

JSONArray 无法根据 indexPath 获取数组

Swift 超出范围.... 使用 tableView[indexPath]

Swift:使用 sectionNameKeyPath 的单元格的 indexPath

在外部函数swift中使用表行中的indexPath