This is my full error
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0})
I have this code for my numberOfRows
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if monthlyExpenses.count == 0{
noExpenseLabel.isHidden = false
}
return monthlyExpenses.count
}
and here is the code for my 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
}
I tried executing the code after CoreDataHelper.save() with a condition
if monthlyExpense.count>=1
but that didn't seem to do anything. Ideally for a month with no expense in it, it should print nothing and a default view which is called NoExpenseLabel, should be visible. Why am I getting this error.
This is a Year View Controller I have, whenever I click on a cell, it performs the code above. So if July is clicked, only expenses in July are displayed. This is why I have the monthly expenses(remove:) code.
Please don't remove monthlyExpenses
in cellForRowAt indexPath
. Because when the tableView call again cellForRowAt indexPath
the array will have less elements that you said in numberOfRowsInSection
. You need to remove your array's elements before and then call reloadData
.