UITableView CoreData - 排序描​​述符不影响标题

时间:2015-08-18 17:13:45

标签: swift uitableview core-data

我向上帝祈祷有人可以帮忙!我已经坚持了几个星期了。

我有一个表示coredata对象的tableview。在表格视图的顶部,我有一个分段控制器,表示不同的数据排序方式。在更改时,分段控制器会更新表的排列。

我有一个问题,在添加数据时,一切看起来都不错 - 直到我更改排序方法。离开“日期”并转到“位置”(例如)后,如果您返回日期,则标题与日期不匹配...日期应按降序显示,标题为MATCH ...

点击“位置”之前 Before changing segmented controller

点击“位置”并返回“日期”后 After changing segmented controller

这是相关的代码(至少我认为是这样......)

@IBAction func segmentedControlChange(sender: AnyObject) {
    //call custom class for overlay loading indicator
    let progressHUD = ProgressHUD(text: "Sorting")
    self.view.addSubview(progressHUD)

    //put the table load on a different thread for processing
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
        self.getFetchResultsController()

        self.tableView.reloadData()

        //after tableview is loaded, execute this
        dispatch_sync(dispatch_get_main_queue(), {
            self.view.sendSubviewToBack(progressHUD)
            progressHUD.hide()
        })
    })
}

func getFetchResultsController(){

    let fetchRequest = NSFetchRequest(entityName: "Place")

    switch self.segmentedControl.selectedSegmentIndex {
    case 0:
        let sortByDate = NSSortDescriptor(key: "date", ascending: false)
        let sortDescriptors = [sortByDate]
        fetchRequest.sortDescriptors = sortDescriptors
        println("sorted by date")
        fetchRequest.fetchBatchSize = 30

        aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: "sectionDateHdr", cacheName: nil)

        aFetchedResultsController.delegate = self

    case 1:
        let sortByState = NSSortDescriptor(key: "state", ascending: true)
        let sortByAOI = NSSortDescriptor(key: "aoiData.aoiName", ascending: true)
        let sortByDate = NSSortDescriptor(key: "date", ascending: false)
        let sortDescriptors = [sortByState, sortByAOI, sortByDate]
        fetchRequest.sortDescriptors = sortDescriptors
        println("sorted by AOI, then date")
        fetchRequest.fetchBatchSize = 30

        aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: "state", cacheName: nil)

        aFetchedResultsController.delegate = self

    case 2:
        let sortByTail = NSSortDescriptor(key: "tailNum", ascending: false)
        let sortByDate = NSSortDescriptor(key: "date", ascending: false)
        let sortDescriptors = [sortByTail, sortByDate]
        fetchRequest.sortDescriptors = sortDescriptors
        println("sorted by tail, then date")
        fetchRequest.fetchBatchSize = 30

        aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: "tailNum", cacheName: nil)

        aFetchedResultsController.delegate = self

    default:
        println("no coredata object to query in fetchData")
    }

    var error: NSError? = nil
    if !self.aFetchedResultsController.performFetch(&error) {
        abort()
    }
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.aFetchedResultsController.sections?.count ?? 0
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return aFetchedResultsController.sections?[section].numberOfObjects ?? 0
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cellIdentifier = "Cell"

    var cell : CustomTableViewCell!
    cell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as! CustomTableViewCell
    var curPlace = aFetchedResultsController.objectAtIndexPath(indexPath) as! AllPlaces

    if curPlace.hasImage == true {
        image = UIImage(data: curPlace.images.imageData)!
    } else {
    image = UIImage(named: "Placeholder")!
    }

    cell.loadItem(curPlace.hasImage, aoi: curPlace.aoiData.aoiName, date: curPlace.date, city: curPlace.city, state: curPlace.state, arrOrDep: curPlace.arrOrDep)
    cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
    cell.selectionStyle = UITableViewCellSelectionStyle.Blue

    if indexPath.row == (aFetchedResultsController.fetchedObjects!.count - 1)
    {
        self.getFetchResultsController()
    }

    return cell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    //self.getFetchResultsController()
    let sectionInfo = self.aFetchedResultsController.sections![section] as! NSFetchedResultsSectionInfo
    return sectionInfo.name
}

1 个答案:

答案 0 :(得分:0)

好的,一旦我陷入困境并寻求帮助,我就会以典型的方式解决这个问题。不知道为什么会这样,但这就是我做的 -

1。)在“AllPlaces”类中,我添加了一个只查看日期的函数,并将MMMM yyyy的值作为字符串返回。我打电话给这个部分ID。

    func sectionID() -> String {
    var aformatter = NSDateFormatter()
    aformatter.dateFormat = "MMMM yyyy"

    return aformatter.stringFromDate(date)
}

2。)我之前使用的是一个名为sectionDateHdr的属性,它是相同“日期”变量的字符串格式,采用相同的MMMM yyyy格式。出于某种原因,使用sectionID而不是sectionDateHdr会导致表正确标记标题。

我真的不知道为什么......但是我很放心它终于工作了。我猜这与设置的值通过fetchResultsController传递时有关。

2周后!!!解决!