菜鸟教程小白 发表于 2022-12-11 18:11:20

ios - 如何按日期从核心数据中获取数据作为一个部分?


                                            <p><pre><code> fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath:&#34;time&#34;, cacheName: nil)
</code></pre>

<p><strong>我正在使用此代码,其中 time 是存储 Date() 对象的列名。</strong></p>

<p>我想获取基于日期的部分
比如……</p>

<blockquote>
<p>29-11-2016 total row = 3</p>

<p>28-11-2016 total row = 1</p>

<p>but currently getting separate section for each time difference on
same date.</p>
</blockquote>

<p><strong>那么,我如何使用获取请求 Controller 来实现这一点。 ????</strong></p>

<p>提前致谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这个问题已经解决了堆栈溢出问题。它会工作,请检查它。</p>

<pre><code>import UIKit
import CoreData

class ExampleViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate {

    @IBOutlet weak var tableView: UITableView!

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
    var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()

    var orders = ()
    var startDate : NSDate = NSDate()
    var endDate : NSDate = NSDate()

    override func viewDidLoad() {
      super.viewDidLoad()
      fetchedResultController.delegate = self
      tableView.dataSource = self
      tableView.delegate = self

      fetchData()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
      return fetchedResultController.fetchedObjects!.count
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      let order = fetchedResultController.fetchedObjects! as! Order
      return ((order.products?.count ?? 0) + (order.services?.count ?? 0))
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let textCellIdentifier = &#34;ExampleTableViewCell&#34;
      let row = indexPath.row

      let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! ExampleTableViewCell

      let order = fetchedResultController.fetchedObjects! as! Order // Data fetched using NSFetchedResultsController
      let products = (order.products?.allObjects ?? ()) as! // Swift Array
      let services = (order.services?.allObjects ?? ()) as! // Swift Array

      if (row &lt; products.count) { // this is a Product row
            cell.orderLabel.text = products.name!
      } else { // this is a Service row
            cell.orderLabel.text = services.name!
      }
      return cell
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
      let order = fetchedResultController.fetchedObjects! as! Order
      return &#34;\(order.date)&#34;
    }


    func orderFetchRequest() -&gt; NSFetchRequest {
      let fetchRequest = NSFetchRequest(entityName: &#34;Order&#34;)
      let sortDescriptor = NSSortDescriptor(key: &#34;date&#34;, ascending: true)
      let predicate = NSPredicate(format: &#34;date &gt;= %@ AND date &lt;= %@&#34;, startDate, endDate) // startDate and endData are defined elsewhere

      fetchRequest.sortDescriptors =
      fetchRequest.predicate = predicate

      return fetchRequest
    }

    func fetchData() {
      let fetchRequest = orderFetchRequest()
      fetchedResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath:nil, cacheName: nil)

      do {
            try fetchedResultController.performFetch()
      }
      catch let error as NSError {
            print(&#34;Could not fetch \(error), \(error.userInfo)&#34;)
      }
    }
}
</code></pre>

<p>我只是复制并粘贴它。希望它对你有用。如果没有,我会帮你的。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何按日期从核心数据中获取数据作为一个部分?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40863588/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40863588/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何按日期从核心数据中获取数据作为一个部分?