首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何按日期作为节从核心数据中提取数据?

如何按日期作为节从核心数据中提取数据?
EN

Stack Overflow用户
提问于 2016-11-29 10:33:43
回答 1查看 2.7K关注 0票数 3
代码语言:javascript
运行
复制
 fetchedResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: moc, sectionNameKeyPath:"time", cacheName: nil)

我正在使用以下代码,其中时间是存储Date()对象的列名.

我想得到日期的部分基础,例如。

29-11-2016共计一行=3 28-11-2016共计一行=1 但目前在同一天,每个时差都有单独的部分。

因此,如何使用请求控制器来实现这一点。?

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-11-29 11:46:16

此问题已在堆栈溢出中得到解决。能用的请检查一下。

代码语言:javascript
运行
复制
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 = [Order]()
    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) -> Int {
        return fetchedResultController.fetchedObjects!.count
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let textCellIdentifier = "ExampleTableViewCell"
        let row = indexPath.row

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

        let order = fetchedResultController.fetchedObjects![indexPath.section] as! Order // Data fetched using NSFetchedResultsController
        let products = (order.products?.allObjects ?? [Product]()) as! [Product] // Swift Array
        let services = (order.services?.allObjects ?? [Service]()) as! [Service] // Swift Array

        if (row < products.count) { // this is a Product row
            cell.orderLabel.text = products[row].name!
        } else { // this is a Service row
            cell.orderLabel.text = services[row-products.count].name!
        }
        return cell
    }

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let order = fetchedResultController.fetchedObjects![section] as! Order
        return "\(order.date)"
    }


    func orderFetchRequest() -> NSFetchRequest {
        let fetchRequest = NSFetchRequest(entityName: "Order")
        let sortDescriptor = NSSortDescriptor(key: "date", ascending: true)
        let predicate = NSPredicate(format: "date >= %@ AND date <= %@", startDate, endDate) // startDate and endData are defined elsewhere

        fetchRequest.sortDescriptors = [sortDescriptor]
        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("Could not fetch \(error), \(error.userInfo)")
        }
    }
}

我只是复制和粘贴它。希望它能对你有用。否则我会帮你的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40863588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档