【发布时间】:2017-07-14 07:43:53
【问题描述】:
我遇到了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
}
【问题讨论】:
-
如果我将 tableView.reloadData() 放在 cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row] 之后,那么模拟器中什么也不会出现......你不应该把 table-view重新加载它。然后它将在无限循环中运行。您不应该强制类型转换为 NSIndexPathm 索引路径行将起作用
-
“我怎样才能解决这个问题..?”——这就是所谓的调试。数组有多少个元素?您要访问哪个元素?为什么数组的元素比你想象的要少? ...想想!
-
@MartinR 你是对的。我是程序员界的新手,所以我会多练习。感谢您的建议....
标签: ios swift uitableview indexpath