第一个 TableView 部分标题不滚动 Swift 4

First TableView section title doesn't scroll Swift 4

我正在为我的应用构建 SalesViewController,它包含一个 TableView,显示在某个日期范围内找到的所有项目。 ItemOrder的child,它有categorydateitemIditemNameprice属性所有 String。 我终于成功地显示了 itemFetchResultController 的结果,因为我错了 sortDescriptor。在配置 itemFetchResultController 时,我想使用 category 属性 从获取的 Item 实体中作为填充的 TableView 中显示的部分标题。我的目标是,面团我不确定是否可能或如何实现它,在其部分中每个 itemName 只有 1 行,但知道在获取中找到了多少行并用它来显示销售价值。这是我第一次使用 sections,这让我有点困惑。我正在尝试遵循 Apple 文档示例代码,但在 tableView 的数据源方法中出现了一些错误,正如您可以通过注释掉的代码看到的那样。我在堆栈溢出上找到的所有其他帖子都非常旧并且在 objective c 中,所以我找不到疑惑的答案。 到目前为止 TableView 填充正确,但第一部分标题在滚动时不会移动。

知道是什么原因造成的吗? 一如既往地非常感谢。

这是我用于 itemFetchResultController 的代码:

lazy var itemFetchedResultController: NSFetchedResultsController<Item> =  {

        // first sortDescriptor filters the date range:  possibly change date from String to dates in both function and  CoreData and use "(date >= %@) AND (date <= %@)" instead of "BEGINSWITH" in predicate
        let itemFetchRequest = NSFetchRequest<Item>(entityName: "Item")
        itemFetchRequest.sortDescriptors = [NSSortDescriptor(key: "category", ascending: true)]
        itemFetchRequest.predicate = NSPredicate(format: "order.user.name == %@", UserDetails.fullName!)
        itemFetchRequest.predicate = NSPredicate(format: "date BEGINSWITH %@", dateToFetch)
        let itemFetchedResultController = NSFetchedResultsController(fetchRequest: itemFetchRequest, managedObjectContext: context, sectionNameKeyPath: "category", cacheName: nil)
        return itemFetchedResultController
    }()

TableView数据来源:

func numberOfSections(in tableView: UITableView) -> Int {

        // apple doc : trhrows an error : Initializer for conditional binding must have Optional type, not 'NSFetchedResultsController<Item>'
//        if let frc = itemFetchedResultController {
//            return frc.sections!.count
//        }
//        return 0

            return itemFetchedResultController.sections!.count

    }

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

        guard let sections = self.itemFetchedResultController.sections else {
            print(" Error :no sections in fetchedResultController" )
            return 0
        }
        let sectionInfo = sections[section]
        return  sectionInfo.numberOfObjects

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "statisticsCell", for: indexPath) as! StatisticsTableViewCell
            cell.idInfoLabel.text = itemFetchedResultController.object(at: indexPath).itemId!
            cell.nameInfoLabel.text = itemFetchedResultController.object(at: indexPath).itemName!

    let item = itemFetchedResultController.object(at: indexPath).itemName!

    let productRequest: NSFetchRequest<Product> = Product.fetchRequest()
    productRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
    productRequest.predicate = NSPredicate(format: "name == %@", item)
    productRequest.fetchLimit = 1
    do {
        let fetch = try context.fetch(productRequest)
        cell.productImageView.image = UIImage(data: (fetch[0].productImage! as Data))
        cell.minimumStockInfoLabel.text = fetch[0].minimumStock
        cell.soldQuantityInfoLabel.text = fetch[0].soldQuantity

    } catch  {
        print("Error in fetching product for cell: \(error)")
    }

    return cell
}

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

//        guard let sections = self.itemFetchedResultController.sections else {
//            print(" Error : no sections in itemsFetchedResultController " )
//            return "empty"
//        }
//        let sectionInfo = sections[section]
//        return sectionInfo.name

        guard let sectionInfo = itemFetchedResultController.sections?[section] else {
            return nil
        }
        return sectionInfo.name

    }

        func sectionIndexTitles(for tableView: UITableView) -> [String]? {
//            if let sectionIndexTitles: FetchedResultController.sectionIndexTitles = self.itemFetchedResultController.sectionIndexTitles {
//                print(" Error : no sections in itemsFetchedResultController " )
//                return [""]
//            }
            return itemFetchedResultController.sectionIndexTitles
        }

    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        // apple doc : trhrows an error : Initializer for conditional binding must have Optional type, not 'Int'
//        guard let result = itemFetchedResultController.section(forSectionIndexTitle: title, at: index) else {
//            fatalError("Unable to locate section for \(title) at index: \(index)")
//        }
//        return result
       let result = itemFetchedResultController.section(forSectionIndexTitle: title, at: index)
        return result

    }

将您的 UITableView 样式设置为分组,您的所有部分将与单元格一起滚动