从核心数据中挑选特定的项目
Picking specific Items from Core Data
我在核心数据中有一个代表锻炼模型的实体。每个实体都有一个 DayID 属性,表示该项目属于哪一天,如图所示。
创建和保存特定项目的代码如下所示 Swift
@IBAction func doneButtonPressed(sender: AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedObjectContextCreation = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("ExerciseItemModel", inManagedObjectContext: managedObjectContextCreation)
//Sunday
let exerciseItemtoAdd = ExerciseItemModel(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContextCreation)
exerciseItemtoAdd.dayID = dayName //DayName could be Wednesday
exerciseItemtoAdd.exerciseType = "Cardio"
exerciseItemtoAdd.exerciseName = self.exerciseNameTextField.text!
exerciseItemtoAdd.durationOrSets = durationLabelTextArea.text!
exerciseItemtoAdd.distanceOrReps = distanceLabelTextArea.text!
exerciseItemtoAdd.weight = ""
exerciseItemtoAdd.date = currentDate
appDelegate.saveContext() //Save the elements I just created.
let request: NSFetchRequest = NSFetchRequest(entityName: "ExerciseItemModel")
do {
_ = try managedObjectContextCreation.executeFetchRequest(request)
// success ...
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}
self.dismissViewControllerAnimated(true, completion: nil)
}
我可以获取特定于一天的项目,例如星期三在 cellForRowAtIndexPath 中使用以下代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let exerciseItem = fetchedResultController.objectAtIndexPath(indexPath) as! ExerciseItemModel
//Check if the DayID is the same as a specific WeekDay e.g. Wednesday
if exerciseItem.dayID == self.weekDayModel.dayName {
//All the code here
}
return Cell!
}
因此我面临的问题是如何将部分函数中的 numbeOfRows 指定为仅 return 与特定 dayName 相关的项目数。例如。我只想要 DayID 为星期三的项目数。
目前,函数 return 所有实体,包括从周日到周六具有其他 dayID 的实体,因此会扭曲 TableView。
这是我的 numberOfRowsInSection 函数:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//let exerciseItem = fetchedResultController.
//if exerciseItem.dayID == self.weekDayModel.dayName {
//}
numberOfExerciseItems = fetchedResultController.sections![section].numberOfObjects
return fetchedResultController.sections![section].numberOfObjects
}
非常感谢任何想法或帮助。 :)
您想像这样向 NSFetchRequest
添加谓词:
let request: NSFetchRequest = NSFetchRequest(entityName: "ExerciseItemModel")
request.predicate = NSPredicate("dayID == %@",self.weekDayModel.dayName)
我在核心数据中有一个代表锻炼模型的实体。每个实体都有一个 DayID 属性,表示该项目属于哪一天,如图所示。
创建和保存特定项目的代码如下所示 Swift
@IBAction func doneButtonPressed(sender: AnyObject) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedObjectContextCreation = appDelegate.managedObjectContext
let entityDescription = NSEntityDescription.entityForName("ExerciseItemModel", inManagedObjectContext: managedObjectContextCreation)
//Sunday
let exerciseItemtoAdd = ExerciseItemModel(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContextCreation)
exerciseItemtoAdd.dayID = dayName //DayName could be Wednesday
exerciseItemtoAdd.exerciseType = "Cardio"
exerciseItemtoAdd.exerciseName = self.exerciseNameTextField.text!
exerciseItemtoAdd.durationOrSets = durationLabelTextArea.text!
exerciseItemtoAdd.distanceOrReps = distanceLabelTextArea.text!
exerciseItemtoAdd.weight = ""
exerciseItemtoAdd.date = currentDate
appDelegate.saveContext() //Save the elements I just created.
let request: NSFetchRequest = NSFetchRequest(entityName: "ExerciseItemModel")
do {
_ = try managedObjectContextCreation.executeFetchRequest(request)
// success ...
} catch let error as NSError {
// failure
print("Fetch failed: \(error.localizedDescription)")
}
self.dismissViewControllerAnimated(true, completion: nil)
}
我可以获取特定于一天的项目,例如星期三在 cellForRowAtIndexPath 中使用以下代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let exerciseItem = fetchedResultController.objectAtIndexPath(indexPath) as! ExerciseItemModel
//Check if the DayID is the same as a specific WeekDay e.g. Wednesday
if exerciseItem.dayID == self.weekDayModel.dayName {
//All the code here
}
return Cell!
}
因此我面临的问题是如何将部分函数中的 numbeOfRows 指定为仅 return 与特定 dayName 相关的项目数。例如。我只想要 DayID 为星期三的项目数。 目前,函数 return 所有实体,包括从周日到周六具有其他 dayID 的实体,因此会扭曲 TableView。
这是我的 numberOfRowsInSection 函数:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//let exerciseItem = fetchedResultController.
//if exerciseItem.dayID == self.weekDayModel.dayName {
//}
numberOfExerciseItems = fetchedResultController.sections![section].numberOfObjects
return fetchedResultController.sections![section].numberOfObjects
}
非常感谢任何想法或帮助。 :)
您想像这样向 NSFetchRequest
添加谓词:
let request: NSFetchRequest = NSFetchRequest(entityName: "ExerciseItemModel")
request.predicate = NSPredicate("dayID == %@",self.weekDayModel.dayName)