我正在使用FRC,我想创建根据日期(DD MMMM)对数据进行分组的部分!每个任务都有一个日期,我正在使用该日期并将其格式化为节标题。
我正在使用他们在Objective C中提供的Apple示例代码,并将其转换为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。它取值为NSDagedObject的cDate值。
然后在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
}
这是我用于其他tableview函数的代码:
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>
为什么我无法在tableview中显示任何部分以及为什么当我删除最后一个tableview单元格时,应用程序崩溃?
谢谢!
答案 0 :(得分:2)
传递给String result = output.contains("1") ? output.substring(output.indexOf("1")) : "0";
的关键路径
获取结果控制器的参数必须是可见的
Objective-C运行时。在Swift 4中需要显式注释
使用sectionNameKeyPath:
(也比较How can I deal with @objc inference deprecation with #selector() in Swift 4?):
@objc
如果没有,则会自动忽略部分名称密钥路径。