Swift 超出范围.... 使用 tableView[indexPath] [英] Swift out of range.... with tableView[indexPath]

查看:24
本文介绍了Swift 超出范围.... 使用 tableView[indexPath]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了超出 range 错误的麻烦...

I'm getting trouble with out of range error...

错误发生在cell.creditLabel.text = numOfDays[(indexPath as NSIndexPath).row] + "days".

如果我把 tableView.reloadData() 放在 cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]

然后模拟器中什么都没有显示......

then nothing shows up in the simulator...

我该如何解决这个问题..?

How can I fix this problem..?

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 :)

You need to check your arrays. From what I can see here, numOfDays have less elements then nameArray. Also, while you are there check creditArray as well :)

此外,也不需要强制转换为 NSIndexPath.

Also, cast to NSIndexPath is not necessary.

这篇关于Swift 超出范围.... 使用 tableView[indexPath]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆