Reputation: 1436
I have a tableView that has data organised by date added, from the latest to the oldest. I want to add a section header that separates/organises each of these cells by date. I have attached a picture of how Fitbit has their section headers. They do their sections in week intervals but I want to show mine separated by days.
This is my code and attempt at it and it doesn't show anything for now though adding a string like "Today" shows that string in the section.
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if let sections = fetchedProgressResultController.sections {
return sections.count
}
return 0
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let numberOfObjects = fetchedProgressResultController.sections?[section].numberOfObjects {
return numberOfObjects
}
return 0
}
//MARK: Section Headers
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let numberOfObjects = fetchedProgressResultController.sections?[section].indexTitle {
return numberOfObjects
}
return nil
//return "TODAY"
}
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel?.font = UIFont(name: "SFUIText-Semibold", size: 15)
header.textLabel?.textColor = UIColor(fromHexString: "3A3E44")
}
Here are the NSFetchRequest functions that show the sorting by date.
//MARK: Progress Model Fetch Function
func progressReportFetchRequest() -> NSFetchRequest {
let fetchRequest = NSFetchRequest(entityName: "ProgressModel")
let sortDescriptor = NSSortDescriptor(key: "exerciseDate", ascending: false) //From latest to Oldest
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
func getProgressReportFetchedResultsController() -> NSFetchedResultsController {
let fetchedProgresssResultController = NSFetchedResultsController(fetchRequest: progressReportFetchRequest(), managedObjectContext: managedProgressObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedProgresssResultController
}
Here is my cellForRowAtIndexPath function showing how the data is pulled
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//Access the different Elements daysOfWeek Array
let progressItems = fetchedProgressResultController.objectAtIndexPath(indexPath) as! ProgressModel
var progressReportCells : ProgressReportTableViewCell! = tableView.dequeueReusableCellWithIdentifier("ProgressReportTableViewCell") as? ProgressReportTableViewCell
if (progressReportCells == nil) {
tableView.registerNib(UINib(nibName: "ProgressReportTableViewCell", bundle: nil), forCellReuseIdentifier:"ProgressReportTableViewCell")
progressReportCells = tableView.dequeueReusableCellWithIdentifier("ProgressReportTableViewCell") as? ProgressReportTableViewCell
}
progressReportCells.workoutTitle.text = progressItems.exerciseName
//Subtitle
progressReportCells.workoutDetails.text = "\(progressItems.distanceOrReps!) \(unitsDefaults.stringForKey(kCCDistanceKeyMetric)!) | \(progressItems.durationOrSets!) min"
//Image
progressReportCells.backgroundImage.image = UIImage(data: progressItems.backgroundExerciseImage!, scale: 1.0)
progressReportCells.backgroundImage.layer.masksToBounds = true
progressReportCells.backgroundImage.clipsToBounds = true
}
This is how my model is organised
extension ProgressModel {
....
@NSManaged var backgroundExerciseImage: NSData?
@NSManaged var distanceOrReps: String?
@NSManaged var exerciseDate: NSDate?
@NSManaged var exerciseName: String?
...
}
Any ideas on how I can do it? Thanks.
Upvotes: 2
Views: 2270
Reputation: 1126
You can do like this
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let numberOfObjects = fetchedProgressResultController.sections?[section].indexTitle {
let df = NSDateFormatter()
df.dateFormat = "yyyy-MM-DD" // Date format for exerciseDate
let exerciseDate = df.dateFromString(numberOfObjects)
let currentDate = NSDate()
let result = NSCalendar.currentCalendar().compareDate(currentDate, toDate: exerciseDate!,
toUnitGranularity: .Day)
let TommorrowDate = currentDate.dateByAddingTimeInterval(60 * 60 * 24)
let result1 = NSCalendar.currentCalendar().compareDate(TommorrowDate, toDate: exerciseDate!,
toUnitGranularity: .Day)
if result == NSComparisonResult.OrderedSame
{
return "TODAY"
}
if result1 == NSComparisonResult.OrderedSame
{
return "TOMMORROW"
}
let df1 = NSDateFormatter()
df1.dateFormat = "dd MMMM, yyyy"
return df1.stringFromDate(exerciseDate!)
}
return nil
//return "TODAY"
}
Upvotes: 1