0

我因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
}
4

3 回答 3

5

您需要检查您的阵列。从我在这里看到的情况来看,numOfDays元素较少nameArray。另外,当你在那里的时候也检查一下creditArray:)

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

于 2017-07-14T07:46:47.637 回答
1

“numOfDays”数组中的元素数小于“nameArray”数组中的元素数

于 2017-07-14T07:49:48.230 回答
1

检查数组是否包含正确数量的项目:

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 的不必要的强制转换(单元格总是会再次绘制,因为它是新的)。

于 2017-07-14T07:59:02.077 回答