我正在尝试创建一个具有两个标题(帐户、支持)的 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
}
最佳答案
首先,您从哪里获取 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/