我的 tableview 目前看起来像这样。
这是 cellForRowAtIndexPath 的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "monthlyCell", for: indexPath) as! MonthlyExpenseTableViewCell
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 64
let expense = expensePerMonth[indexPath.row]
let date = expense.modificationDate
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE d MMMM"
let dateString = dateFormatter.string(from: date! as Date).uppercased()
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
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(red:0.49, green:0.83, blue:0.13, alpha:1.0)
}
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
}
如您所见,费用按日期分组。当我删除顶部的费用时,我希望下一个费用接管并显示日期标签。这是顶部的标签,显示日期,对于某个日期已经存在的费用是不可见的。但是,当我删除一项费用时,我会在这一行得到一个 EXC_BAD_INSTRUCTION:
let dateString = dateFormatter.string(from: date! as Date).uppercased()
可能是因为日期标签对单元格不可见。
那么我该怎么做呢?
删除单元格的代码是这样的:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
let collection = monthlyExpenses[indexPath.row]
CoreDataHelper.deleteExpense(expense: collection)
monthlyExpenses.remove(at: indexPath.row )
monthlyExpenses = CoreDataHelper.retrieveExpenses()
}
}