在 tableView 中添加日期作为部分 header

Adding a date as a section header in a tableView

我有一个 tableView,其中包含按添加日期从最新到最旧组织的数据。我想添加一个 header 部分 separates/organises 每个单元格的日期。我附上了 Fitbit 如何拥有其部分 headers 的图片。他们每隔一周做一次他们的部分,但我想展示我的部分,间隔几天。

这是我的代码并尝试使用它,但它现在没有显示任何内容,尽管添加一个像 "Today" 这样的字符串会在该部分中显示该字符串。

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")

    }

这里是显示按日期排序的 NSFetchRequest 函数。

//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

    }

这是我的 cellForRowAtIndexPath 函数,显示了如何提取数据

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


}

我的模型是这样组织的

extension ProgressModel {
    ....
    @NSManaged var backgroundExerciseImage: NSData?
    @NSManaged var distanceOrReps: String?
    @NSManaged var exerciseDate: NSDate?
    @NSManaged var exerciseName: String?
    ...
}

关于我该怎么做的任何想法?谢谢。

你可以这样做

 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"
    }