2016-12-06 38 views
0
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

检查indexpath部分和行的设置位置。如果它在循环中,请检查增量。 –

回答

1

在numberOfRowsInSection要返回items.count这将是整个阵列的数量在你的项目阵......这实际上是sections.count这是不对的数量。这应该是items[section].count

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items[section].count 
}