gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:40:06 26 4
gpt4 key购买 nike

我正在为我的应用构建 SalesViewController,它包含一个 TableView,显示在某个日期范围内找到的所有项目。ItemOrder 的子项,它有 categorydateitemIditemNameprice属性都是String。我终于成功地显示了 itemFetchResultController 的结果,因为我有错误的 sortDescriptor。在配置 itemFetchResultController 时,我想使用获取的 Item 实体中的 category 属性作为填充的 TableView 中显示的部分标题。我的目标是,面团我不确定是否可能或如何实现它,每个 itemName 在其部分中只有 1 行,但知道在获取和使用中发现了多少行它显示销售值(value)。这是我第一次使用 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 样式设置为分组,您的所有部分将与单元格一起滚动

enter image description here

关于swift - 第一个 TableView 部分标题不会滚动 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56165078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com