用swift做的一个DEMO,写的假数据,可以运行,但是刚运行就崩溃,报错信息是:[__NSArrayM objectAtIndex:]: index 18 beyond bounds [0 .. 17]跪求大神帮助,小弟叩谢. 代码稍长...
var data:NSArray?//传递过来已经组织好的数据
var tempData:NSMutableArray?//用于存储数据源
init(frame:CGRect,data:NSArray) {
super.init(frame: frame, style: UITableViewStyle.grouped)
self.delegate = self
self.dataSource = self
self.data = data
self.tempData = self.createTempData(data: data) as NSMutableArray
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//初始化数据源
func createTempData(data:NSArray) -> NSMutableArray{
let tempArray = NSMutableArray()
for i in 0...data.count {
let node:Node = data.object(at: i) as! Node
if (node.expand)! {
tempData?.adding(node)
}
}
return tempArray
}
//MARK:UITableViewDataSource
//MARK:Required
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (tempData?.count)!
}
//以下是写的假数据以及TableView
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.initData()
}
func initData(){
let country1:Node = Node.init(parentID: -1, nodeID: 0, name: "中国", depth: 0, expand: true)
let province1:Node = Node.init(parentID: 0, nodeID: 1, name: "陕西", depth: 1, expand: false)
let city1:Node = Node.init(parentID: 1, nodeID: 2, name: "西安", depth: 2, expand: false)
let county1:Node = Node.init(parentID: 2, nodeID: 3, name: "长安", depth: 3, expand: false)
let county2:Node = Node.init(parentID: 2, nodeID: 4, name: "韦曲", depth: 3, expand: false)
let city2:Node = Node.init(parentID: 1, nodeID: 5, name: "宝鸡", depth: 2, expand: false)
let county3:Node = Node.init(parentID: 5, nodeID: 6, name: "扶风", depth: 3, expand: false)
let county4:Node = Node.init(parentID: 5, nodeID: 7, name: "眉县", depth: 3, expand: false)
let province2:Node = Node.init(parentID: 0, nodeID: 8, name: "浙江", depth: 1, expand: false)
let city3:Node = Node.init(parentID: 8, nodeID: 9, name: "杭州", depth: 2, expand: false)
let city4:Node = Node.init(parentID: 8, nodeID: 10, name: "嘉兴", depth: 2, expand: false)
let county5:Node = Node.init(parentID: 10, nodeID: 11, name: "嘉善", depth: 3, expand: false)
let county6:Node = Node.init(parentID: 10, nodeID: 12, name: "南湖", depth: 3, expand: false)
let country2:Node = Node.init(parentID: -1, nodeID: 13, name: "美国", depth: 0, expand: true)
let province3:Node = Node.init(parentID: 13, nodeID: 14, name: "德州", depth: 1, expand: false)
let city5:Node = Node.init(parentID: 13, nodeID: 15, name: "休斯顿", depth: 2, expand: false)
let city6:Node = Node.init(parentID: 13, nodeID: 16, name: "圣安东尼奥", depth: 2, expand: false)
let county7:Node = Node.init(parentID: 16, nodeID: 17, name: "邓肯", depth: 3, expand: false)
let data:NSMutableArray = [country1,country2,province1,province2,province3,city1,city2,city3,city4,city5,city6,county1,county2,county3,county4,county5,county6,county7]
let rect = CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: self.view.frame.size.height)
let tableView:TreeTableView = TreeTableView.init(frame: rect, data: data)
self.view.addSubview(tableView)
}
Pour la boucle ici,
for i in 0...data.count
, vous l'écrivez comme ceci :0
àdata.count
, mais ce dont vous avez réellement besoin c'est de0
àdata.cout-1
, donc cela fera sortir le tableau des limites .Changez la boucle for en
for i in 0..<data.count
et c'est tout