我的tableview目前看起来像这样。
这里是cellForRowAtIndexPath的代码
f64.const
}
如您所见,费用按日期分组。当我删除顶部的费用时,我希望接下来的费用来接管并显示datelabel。这是顶部的标签,显示日期,对于某个特定日期已经存在的费用是不可见的。但是,当我删除费用时,我会在此行收到一个EXC_BAD_INSTRUCTION:
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
可能是因为日期标签对于单元格是不可见的。
那我该怎么做呢?
删除单元格的代码是:
let dateString = dateFormatter.string(from: date! as Date).uppercased()