gpt4 book ai didi

ios - Swift - 一对多排序

转载 作者:行者123 更新时间:2023-11-29 00:17:12 27 4
gpt4 key购买 nike

我有两个实体:

游泳池参数

swimmingPool 与参数有关系(每个池可以有多个参数,因此是一对多关系)。

在我的主视图 Controller 中,我正在为池获取数据。现在我通过 segue 将数据传递给第二个 View Controller ,它包含与特定池相关的所有操作的列表。从这里我将数据发送到第三个 View Controller ,它有一个与池相关的所有参数的列表。

我能够获取数据并显示它们,但似乎无法对它们进行排序。

这是在我的主 VC 中获取池的代码:

func attemptFetch() {

let fetchRequest: NSFetchRequest<SwimminPool> = SwimminPool.fetchRequest()
let dataSort = NSSortDescriptor(key: "poolCreatedAt", ascending: false)
fetchRequest.sortDescriptors = [dataSort]


let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

controller.delegate = self
self.controller = controller

do {
try controller.performFetch()
} catch {
let error = error as NSError
print("\(error.debugDescription)")
}

}

传递数据的segue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MyParams" {
if let destination = segue.destination as? MyParamsVC {
destination.swimmingPool = swimmingPoolHome
}
}
}

这是在第三个 Controller 中配置单元的代码:

var swimmingPool: SwimmingPool! // data received from the segue

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ParamCell", for: indexPath) as! ParamCell
configureCell(cell: cell, indexPath: indexPath as NSIndexPath)
return cell
}
func configureCell(cell: ParamCell, indexPath: NSIndexPath) {
let param = swimmingPool.parameters?.allObjects[indexPath.row]
cell.configureCell(parameter: param as! Parameter)
}

最后是我的 paramCell:

lass ParamCell: UITableViewCell {

@IBOutlet weak var paramCreatedAt: UILabel!
@IBOutlet weak var noteLbl: UILabel!

func configureCell(parameter: Parameter) {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MMMM-yyyy, HH:mm:ss"
let dateString = formatter.string(from: parameter.createdAt!).capitalized
paramCreatedAt.text = dateString
noteLbl.text = parameter.paramNotes
}

关于如何根据日期对数据进行排序有什么想法吗?

最佳答案

MyParamsVC中创建一个单独的数据源数组:

var parameters = [Parameter]()

didSet 观察器添加到 swimmingPool 中,它获取 Parameter 实例并对其进行排序(将 createdAt 更改为您的所需的属性)

var swimmingPool: SwimmingPool!  { // data received from the segue
didSet {
parameters = (swimmingPool.parameters?.allObjects as! [Parameter]).sorted(by: {$0.createdAt! < $1.createdAt!})
}
}

cellForRowAtconfigureCell 替换为(我将 NSIndexPath 更改为 Swift 3 IndexPath)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ParamCell", for: indexPath) as! ParamCell
configureCell(cell: cell, indexPath: indexPath)
return cell
}

func configureCell(cell: ParamCell, indexPath: IndexPath) {
let param = parameters[indexPath.row]
cell.configureCell(parameter: param)
}

关于ios - Swift - 一对多排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45061624/

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