ios - 无法使用 '[Array<String>]' 类型的索引为 '(section)' 类型的值添加下标。类型'(又名 'section.Type' )

标签 ios xcode swift uitableview uitableviewsectionheader

我正在尝试创建一个具有两个标题(帐户、支持)的 TableView 并在各部分中添加文本标题。但我收到错误

我遵循了这个问题的答案:How do I populate two sections in a tableview with two different arrays using swift?

错误: enter image description here 和: enter image description here

这是我的代码:

 var myTitles = [["Delete current photo","Change profile picture","Change username","Change password"],["Help","Report a problem"]]

let headerTitles = ["Acount", "Support"]

override func viewDidLoad() {
    super.viewDidLoad()

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return myTitles[section].count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return 3 //This is not complete yet.
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section < headerTitles.count {
        return headerTitles[section]
    }

    return nil
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("settingCell", forIndexPath: indexPath)

    cell.textLabel?.text = myTitles[indexPath.section][indexPath.row]

    return cell
}

最佳答案

首先,您从哪里获取 section 值:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return myTitles[section].count
}

将其更改为仅返回 myTitles.count

关于ios - 无法使用 '[Array<String>]' 类型的索引为 '(section)' 类型的值添加下标。类型'(又名 'section.Type' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36361232/

相关文章:

iphone - 在 iPhone 上获取距离矩阵 API (google) 的 XML

ios - 无法构建到 iPhone Swift Tesseract ocr

ios - NSUbiquityIdentityDidChangeNotification 和 SIGKILL

objective-c - 如何在适用于 iOS 的新版 XCode 6.0.1 (6A317) 中创建 Objective-C 协议(protocol)

ios - 点击搜索栏时,Tableview 单元格消失 - Swift

ios - 获取多维数组的键( `[String: [String: String]]`)

ios - Swift Package Manager添加了自动第三方库确认和许可

ios - 根据通过 NSNotification 接收的索引从 TableView 中删除特定行

ios - 在 Swift 中以编程方式创建 UITableView

objective-c - 新手询问从 NSMutableArray 读取 NSMutableArray 并将 NSMutableArray 保存到文档文件