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
}
}
上記のコードは、一時プロパティsectionIdentifierを作成しているNSManagedObjectにあります。NSManagedObjectである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
}
これは、他のテーブルビュー関数に使用しているコードです。
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>
テーブルビューにセクションを表示できないのはなぜですか?また、最後のテーブルビューセルを削除すると、アプリがクラッシュするのはなぜですか?
ありがとう!
sectionNameKeyPath:
フェッチされた結果コントローラーの引数に渡されるキーパスは、Objective-Cランタイムから見える必要があります。明示的なアノテーションを必要とするSwift4では(Swift 4の#selector()で@objc推論の非推奨に対処@objc
するにはどうすればよいですか?):
extension completedNotes {
@objc var sectionIdentifier : String? {
// ...
}
}
それがないと、セクション名のキーパスは黙って無視されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加