【发布时间】:2023-03-03 23:32:02
【问题描述】:
我有一个跟踪每月支出的应用程序。我有一个 Expense 实体,该实体具有一个 month 属性,用于跟踪创建当前月份的费用。然后,我将在表格视图中显示每个月的费用,如下所示。用户只有在下个月或上个月有费用时才能左右切换
@IBAction func backMonthButtonPressed(sender: AnyObject) {
print("Back Button Pressed")
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentMonth, options: [])!
if checkMonth(currentMonth) {
updateFetch()
setMonth()
} else {
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentMonth, options: [])!
}
tableView.reloadData()
}
@IBAction func nextMonthButtonPressed(sender: AnyObject) {
print("Next Button Pressed")
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: 1, toDate: currentMonth, options: [])!
if checkMonth(currentMonth) {
updateFetch()
setMonth()
} else {
currentMonth = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: currentMonth, options: [])!
}
tableView.reloadData()
}
func checkMonth(month : NSDate) -> Bool {
let app = UIApplication.sharedApplication().delegate as! AppDelegate
let context = app.managedObjectContext //scratch pad
let fetchRequest = NSFetchRequest(entityName: "Expense")
fetchRequest.predicate = NSPredicate(format: "month == %@", month.MonthYearDateFormatter())
let count = context.countForFetchRequest(fetchRequest, error: nil)
if count > 0 {
print("There are expenses for this month \(month.MonthYearDateFormatter()). Show expenses")
return true
} else {
print("There are no expenses for this month \(month.MonthYearDateFormatter()). Do Nothing")
return false
}
}
我的问题是,在不太可能的情况下,用户在 6 月份创建了一项费用,但在 8 月份没有创建费用。我怎样才能让用户在不跳过它的情况下仍然看到他/她在 8 月份的费用。有任何想法吗?
【问题讨论】:
-
一次获取所有费用并将它们存储在一个数组中,按月排序。然后只需浏览数组
标签: ios swift core-data swift2 nscalendar