【发布时间】:2018-03-11 14:54:11
【问题描述】:
我正在使用 FRC,我想创建根据日期 (DD MMMM) 对数据进行分组的部分!每个任务都有一个日期,我正在使用该日期并将其格式化为节标题。
我正在使用 Apple 在 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 的值,即 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>
为什么我无法让任何部分显示在 tableview 中,以及为什么当我删除最后一个 tableview 单元格时,应用程序崩溃?
谢谢!
【问题讨论】:
-
我没有在这里使用故事板。只需编码!
-
不!不使用 .grouped 样式属性。
-
我正在创建这样的表格视图:
let tableView : UITableView = { let tv = UITableView() tv.separatorColor = .clear return tv }()
标签: ios swift uitableview core-data nsfetchedresultscontroller