Swift 超出范围....使用 tableView[indexPath]
Swift out of range.... with tableView[indexPath]
我遇到 range
错误...
错误发生在cell.creditLabel.text = numOfDays[(indexPath as NSIndexPath).row] + "days"
。
如果我把 tableView.reloadData()
放在 cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]
之后
然后模拟器中什么也没有显示...
我该如何解决这个问题..?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : MoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MoneyTableViewCell", for: indexPath) as! MoneyTableViewCell
cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]
cell.creditLabel.text = numOfDays[(indexPath as NSIndexPath).row] + "days"
cell.levelLabel.text = creditArray[(indexPath as NSIndexPath).row]
cell.layoutIfNeeded()
return cell
}
您需要检查您的数组。从我在这里看到的情况来看,numOfDays
的元素比 nameArray
少。此外,当你在那里时,也检查 creditArray
:)
此外,不需要强制转换为 NSIndexPath
。
"numOfDays" 数组中的元素数小于 "nameArray" 数组中的元素数
检查数组中的项目数量是否正确:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : MoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MoneyTableViewCell", for: indexPath) as! MoneyTableViewCell
cell.nameLabel.text = nameArray[indexPath.row]
if numOfDays.count < indexPath.row {
cell.creditLabel.text = numOfDays[indexPath.row] + "days"
}
if creditArray.count < indexPath.row {
cell.levelLabel.text = creditArray[indexPath.row]
}
return cell
}
此外,我删除了不必要的 indexPath 和 layoutIfNeeded 转换(单元格总是会再次绘制,因为它是新的)。
我遇到 range
错误...
错误发生在cell.creditLabel.text = numOfDays[(indexPath as NSIndexPath).row] + "days"
。
如果我把 tableView.reloadData()
放在 cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]
之后
然后模拟器中什么也没有显示...
我该如何解决这个问题..?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : MoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MoneyTableViewCell", for: indexPath) as! MoneyTableViewCell
cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]
cell.creditLabel.text = numOfDays[(indexPath as NSIndexPath).row] + "days"
cell.levelLabel.text = creditArray[(indexPath as NSIndexPath).row]
cell.layoutIfNeeded()
return cell
}
您需要检查您的数组。从我在这里看到的情况来看,numOfDays
的元素比 nameArray
少。此外,当你在那里时,也检查 creditArray
:)
此外,不需要强制转换为 NSIndexPath
。
"numOfDays" 数组中的元素数小于 "nameArray" 数组中的元素数
检查数组中的项目数量是否正确:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.nameArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : MoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MoneyTableViewCell", for: indexPath) as! MoneyTableViewCell
cell.nameLabel.text = nameArray[indexPath.row]
if numOfDays.count < indexPath.row {
cell.creditLabel.text = numOfDays[indexPath.row] + "days"
}
if creditArray.count < indexPath.row {
cell.levelLabel.text = creditArray[indexPath.row]
}
return cell
}
此外,我删除了不必要的 indexPath 和 layoutIfNeeded 转换(单元格总是会再次绘制,因为它是新的)。