所以我的ViewController中有多个tableView,我以这样的方式创建了原型单元,我应该能够添加到数组中,tableViews会添加尽可能多的部分,但是我的数组中有当我尝试将第4个项目添加到我的数组时,我得到了一个范围错误的索引。
设置数组:
var upgradeNames = ["Agility", "Strength", "Endurance", "intelligence"]
var premiumNames = ["Coins", "Gold", "Stuff"]
var upgradeItemImage = [UIImage(named: "upgrade"), UIImage(named: "premium"), UIImage(named: "settings"), UIImage(named: "upgrade")]
var upgradeLevel = [0, 0, 0, 0]
var upgradeCost = [10, 20, 40, 80]
var upgradeDps = [0.1, 0.2, 0.4, 0.8]
多个tableView单元格设置:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == self.upgradesTableView {
let cell = self.upgradesTableView.dequeueReusableCellWithIdentifier("upgradeCell", forIndexPath: indexPath) as! CustomCell
cell.upgradeDescriptionLabel.text = upgradeNames[indexPath.section]
cell.upgradeLevelLabel.text = "Lvl: \(upgradeLevel[indexPath.section])"
cell.tapAction = { (cell) in
if self.score >= Double(self.upgradeCost[indexPath.section]) {
self.score = Double(self.score - Double(self.upgradeCost[indexPath.section]))
self.scoreLabel.text = "Skill Points: \(String(format: "%.1f", self.score))"
self.upgradeLevel[indexPath.section] += 1
self.dps += Double(self.upgradeDps[indexPath.section])
tableView.reloadData()
}
else {
return
}
}
return cell
}
if tableView == self.premiumTableView {
let premiumCell = self.premiumTableView.dequeueReusableCellWithIdentifier("premiumCell", forIndexPath: indexPath) as! PremiumCell
premiumCell.descriptionLabel.text = premiumNames[indexPath.section]
premiumCell.tapAction = { (cell) in
//TODO: Add Premium Upgrade Bought Logic
}
return premiumCell
}
else {
return UITableViewCell()
}
}
这里是我在视图中设置部分数量的地方。如果我放 return upgradeNames.count - 1
错误消失但我的阵列缺少第四个索引片段。
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if tableView == upgradesTableView {
return upgradeNames.count
}
if tableView == premiumTableView {
return premiumNames.count
}
else {
return 1
}
}
这里是numberofRowsInSection:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
非常感谢任何帮助。