繁体   English   中英

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,- Swift 3

[英]Terminating app due to uncaught exception 'NSInternalInconsistencyException',- Swift 3

这是我的全部错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})

我的 numberOfRows 有这个代码

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if monthlyExpenses.count == 0{
        noExpenseLabel.isHidden = false
    }
    return monthlyExpenses.count
} 

这是我的 cellForRowAtIndexPath 的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "monthlyCell", for: indexPath) as! MonthlyExpenseTableViewCell

    //var newMode2:Bool? = nil

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 64

    let expense = monthlyExpenses[indexPath.row]

    let date = expense.modificationDate
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "EEEE d MMMM"
    let dateString = dateFormatter.string(from: date! as Date).uppercased()

    for expense in monthlyExpenses {
        if expense.modificationDate?.convertToMonth() != monthDisplayed.text{
            if let expenseSorter = monthlyExpenses.index(of: expense) {
                monthlyExpenses.remove(at: expenseSorter)
            }
        }
    }
    CoreDataHelper.save()
    if indexPath.row == 0{
        newMode = true
    }
    if indexPath.row>=1{
        monthlyExpenses.sorted(by: { $0.modificationDate as! Date > $1.modificationDate as! Date})
      let previousExpensesData = monthlyExpenses[indexPath.row - 1].modificationDate
        let day = Calendar.current.component(.day, from: monthlyExpenses[indexPath.row].modificationDate as! Date) // Do not add above 'date' value here, you might get some garbage value. I know the code is redundant. You can adjust that.
        let previousDay = Calendar.current.component(.day, from: monthlyExpenses[indexPath.row - 1].modificationDate! as Date)
        if day == previousDay {
            newMode = false
        } else {
            newMode = true
        }
    }


    var expenseAmountCalculating:Double = Double(expense.amount!)!
    var expenseAmountDisplayed:String = convertToMoney(expenseAmountCalculating)

    var finalDisplayed:String = expense.currencySymbol! + " " + expenseAmountDisplayed
    let screenSize: CGRect = UIScreen.main.bounds
    cell.dateLabel.text = dateString
    cell.expenseName2.text = expense.name
    cell.expenseAmount2.text = finalDisplayed
    cell.expenseCategory2.text = expense.category
    cell.expenseCollection2.text = expense.collection
                if (expense.expense) {
                    cell.expenseAmount2.textColor = UIColor.red
                }


                else if (expense.income){
                    cell.expenseAmount2.textColor = UIColor.green
                }

                if (expense.cash) && (expense.expense){
                    cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Expense Icon")

                }
                else if (expense.cash) && (expense.income){
                    cell.cashOrCredit.image = #imageLiteral(resourceName: "Cash-Income Icon")

                }
                else if (expense.credit) && (expense.income){
                    cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Income Icon")

                }
                else if (expense.credit) && (expense.income){
                    cell.cashOrCredit.image = #imageLiteral(resourceName: "Credit-Expense Icon")

                }
    if newMode == false{
        cell.dateLabel.isHidden = true
        tableView.rowHeight = 64
        cell.expenseName2.frame.origin = CGPoint(x: 60, y: 11)
        cell.expenseCategory2.frame.origin = CGPoint(x: 61, y: 33)
        cell.expenseCollection2.frame.origin = CGPoint(x: 108, y: 33)


    }
    else if newMode == true{
        tableView.estimatedRowHeight = 64

    }

return cell

}

我尝试在 CoreDataHelper.save() 之后使用条件执行代码

if monthlyExpense.count>=1

但这似乎没有任何作用。 理想情况下,一个月没有任何费用,它应该不打印任何内容,并且一个名为 NoExpenseLabel 的默认视图应该是可见的。 为什么我收到这个错误。

这是我拥有的年视图控制器,每当我单击一个单元格时,它都会执行上面的代码。 因此,如果单击“七月”,则仅显示七月的费用。 这就是为什么我有每月费用(删除:) 代码。

在此处输入图片说明

请不要删除monthlyExpensescellForRowAt indexPath 因为当 tableView 再次调用cellForRowAt indexPath ,数组中的元素将少于您在numberOfRowsInSection所说的元素。 您需要先删除数组的元素,然后再调用reloadData

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM