【问题标题】:Cannot subscript a value of type '[Array<String>]' with index of type '(section)'. Type' (aka 'section.Type')无法使用索引类型为“(section)”的“[Array<String>]”类型的值下标。类型”(又名“section.Type”)
【发布时间】:2016-04-01 16:23:30
【问题描述】:

我正在尝试创建一个带有两个标题(帐户、支持)的 TableView 并在部分中带有文本标题。但是我遇到了错误

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

错误: 和:

这是我的代码:

 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
}

【问题讨论】:

    标签: ios xcode swift uitableview uitableviewsectionheader


    【解决方案1】:

    首先,您在哪里获得 section 值:

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

    将其更改为 return myTitles.count

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      相关资源
      最近更新 更多