class ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {
var sections = ["A","B","C","D"]
var items = [["General","Privacy",""],["icloud","Maps","News"],["safari","Photos and Camera","Game Center"],["Twitter","Facebook","Flicker"]]
var secItems = ["iclouds","Maps","Safari"]
var thirItems = ["Twitter","General","Privacy","icloud"]
var imgItems = ["settings_ios7_ios_7.png","privacy.jpeg","icloud.png","Google-Maps-4.0-for-iOS-app-icon-small.png","news.jpeg","safari_ios7_ios_7 (1).png","photos.jpeg","game_center_ios7_ios_7.png","twitter.jpeg","facebook.jpeg","flicker.png"]
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Settings"
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return sections.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "ABC"
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! TableViewCell
cell.lblName.text = items[indexPath.section][indexPath.row]
cell.imgName.image = UIImage(named: imgItems[indexPath.row])
cell.arrow.image = UIImage(named: "arrowIcon.png")
return cell
}
}
在此处获取错误index out of range
:
cell.lblName.text = items[indexPath.section][indexPath.row]
(当我们采用4乘4多维数组时,它工作正常,但我们采用不同的数组得到index out of range
。
答案 0 :(得分:1)
在numberOfRowsInSection中,您将返回items.count
,这将是您的Item数组中的总数组的数量...这实际上是sections.count的数量,这是错误的。它应该是items[section].count
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].count
}