【问题标题】:UITableView header dates using NSFetchedResultsController - Swift?UITableView 标题日期使用 NSFetchedResultsController - Swift?
【发布时间】:2015-01-20 16:56:13
【问题描述】:

我正在从 Core Data 中提取字符串值并将它们显示在 UITableView 中……这正在工作并显示所有条目。我现在想按创建日期(也存储在 Core Data 中)对这些值进行“分组”,但很难实现这个组标题......这就是我所拥有的:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var sound = self.sounds[indexPath.row]
        var cell = UITableViewCell()

        // Convert Date to String
        var formatter: NSDateFormatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
        let dateTimePrefix: String = formatter.stringFromDate(sound.dateSaved)
        // Display the following in each rows
        cell.textLabel!.text = sound.name + " Saved: " + dateTimePrefix

        return cell
    }

有人可以为我指出如何实现“标题”的正确方向吗?我已经将 Main.storyboard 中的 tableView 设置为“分组”作为样式。

--- 更新

感谢下面的回复,但不幸的是,示例在 Obj-C 中,我正在为 swift 拍摄。

为了展开,我从 Core Data 中检索数据并在 UITableView 中显示所有数据:

override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.dataSource = self
        self.tableView.delegate = self
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    var context = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
    var request = NSFetchRequest(entityName: "Sound")
    self.sounds = context.executeFetchRequest(request, error: nil)! as [Sound]
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.sounds.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
    var sound = self.sounds[indexPath.row]

    // Convert Date to String
    var formatter: NSDateFormatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd @ HH:mm:ss"
    let dateTimePrefix: String = formatter.stringFromDate(sound.dateSaved)
    // Display the following in each rows
    cell.textLabel!.text = sound.name // Sound Name
    // Display the following as each subtitles
    cell.detailTextLabel!.text = dateTimePrefix // Sound Duration (Currently 'date')

    return cell
}

我相信有人可以指出我如何使用sectionNameKeyPath 使用“月份”标题按月“分组”每个月的正确方向吗?

【问题讨论】:

    标签: ios uitableview date swift core-data


    【解决方案1】:

    您正在寻找sectionNameKeyPath,特别是根据日期计算出的部分标题。

    iPhone: Group by month - Core Data https://developer.apple.com/library/ios/samplecode/DateSectionTitles/Introduction/Intro.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多