NSFetchedResultsController titleForHeaderInSection with formatted NSDate

前端 未结 3 1615
一生所求
一生所求 2021-02-10 10:22

In my Core Data app I am using a FetchedResultsController. Usually to set titles for headers in a UITableView you would implement the following method like so:

-         


        
3条回答
  •  我寻月下人不归
    2021-02-10 10:27

    This is the Swift version of the answer:

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {        
        let sectionInfo = fetchedResultsController.sections![section]
        let objects = sectionInfo.objects
        if let topRecord:NSManagedObject = objects![0] as? NSManagedObject  {
            let formatter = DateFormatter()
            formatter.dateStyle = .medium
            return formatter.string(from: topRecord.value(forKey: "itemDate") as! Date)
        } else {
            return sectionInfo.indexTitle
        }
    }
    

提交回复
热议问题