我正在使用FRC,想要创建根据日期(DD MMMM)分组数据的部分!每个任务都有一个日期,我正在使用该日期并将其格式化为部分标题标题。
我正在使用苹果在Objective C中提供的示例代码,并将其转换为swift。以下是代码:
import UIKit
import CoreData
class completedNotes: NSManagedObject
{
@NSManaged var cNote: String
@NSManaged var cPriorityColor: UIColor
@NSManaged var cDate: Date
}
extension completedNotes {
var sectionIdentifier : String? {
// Create and cache the section identifier on demand.
self.willAccessValue(forKey: "sectionIdentifier")
var tmp = self.primitiveValue(forKey: "sectionIdentifier") as? String
self.didAccessValue(forKey: "sectionIdentifier")
if tmp == nil {
if let timeStamp = self.value(forKey: "cDate") as? NSDate {
/*
Sections are organized by month and year. Create the section
identifier as a string representing the number (year * 1000) + month;
this way they will be correctly ordered chronologically regardless
of the actual name of the month.
*/
let calendar = NSCalendar.current
let components = calendar.dateComponents([.year, .month,], from: timeStamp as Date)
tmp = String(format: "%ld", components.year! * 1000 + components.month!)
self.setPrimitiveValue(tmp, forKey: "sectionIdentifier")
}
}
return tmp
}
}
以上代码位于NSManagedObject中,我正在创建一个瞬态属性sectionIdentifier,它取cDate的值,cDate是NSManagedObject。
然后在titlesforheaderinSection方法中:
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
struct Formatter {
static let formatter : DateFormatter = {
let fmt = DateFormatter()
let dateFormat = DateFormatter.dateFormat(fromTemplate: "MMMM yyyy", options: 0,
locale: NSLocale.current)
fmt.dateFormat = dateFormat
return fmt
}()
}
if let theSection = fetchedResultsController1.sections?[section] as? NSFetchedResultsSectionInfo,
let numericSection = Int(theSection.name) {
var components = DateComponents()
components.year = numericSection / 1000
components.month = numericSection % 1000
if let date = Calendar.current.date(from: components) {
let titleString = Formatter.formatter.string(from: date)
return titleString
}
}
return nil
}
这是我用于其他表格视图功能的代码:
func numberOfSections(in tableView: UITableView) -> Int {
return (fetchedResultsController1.sections?.count)!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fetchedResultsController1.sections![section].numberOfObjects
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellid, for: indexPath) as! TasksTableViewCell
cell.selectionStyle = .none
cell.textview.text = fetchedResultsController1.object(at: indexPath).cNote
cell.priorityline.backgroundColor = fetchedResultsController1.object(at: indexPath).cPriorityColor
return cell
}
这是NSFetchedResultsController:
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.moContext1, sectionNameKeyPath: "sectionIdentifier", cacheName: nil)
aFetchedResultsController.delegate = self
_fetchedResultsController = aFetchedResultsController as? NSFetchedResultsController<completedNotes>
为什么表格视图中没有任何部分显示,并且当我删除最后一个表格视图单元格时,应用程序会崩溃?
谢谢!
let tableView: UITableView = { let tv = UITableView() tv.separatorColor = .clear return tv }()
- Osama Naeem