如何以编程方式根据排序条件对单元格重新排序 SWIFT 4
How to reorder cells based on sort criteria programatically SWIFT 4
我正在尝试根据将由段控件触发的排序标准对单元格重新排序。我在排序时遇到的问题只是对 table 中的标签进行排序,但是文本将根据用户交互更改的按钮等某些元素保持在同一行,这没有意义,应该更多细胞本身。请在点击距离段控件时查看差异 它正确排序数据 问题是按钮 START ROUTE 在第 2 行时被按下并更改为 IN TRANZIT 现在排序它应该在第 1 行但事实并非如此这里的情况。填充 table 的数据来自核心数据。有人能给我指出正确的方向吗?
func sortData() {
if segment.selectedSegmentIndex == 1 {
dropOffLocations.sort(by: {[=11=].dateCreated! < .dateCreated! })
} else if segment.selectedSegmentIndex == 2 {
dropOffLocations.sort(by: {[=11=].distance < .distance })
} else if segment.selectedSegmentIndex == 3 {
dropOffLocations.sort(by: {[=11=].postcode! < .postcode! })
}
tableView.reloadData()
}
@IBAction func segmentChange(_ sender: Any) {
sortData()
}
@objc func tappedButton(sender : UIButton) {
if let cell = sender.superview?.superview?.superview as? UITableViewCell {
if let indexPath = tableView.indexPath(for: cell) {
let dropOffLocation = dropOffLocations[indexPath.row]
sender.setTitle("IN TRANZIT", for: .normal)
sender.titleLabel?.adjustsFontSizeToFitWidth = true
dropOffLocation.btnStatus = sender.titleLabel?.text
sender.backgroundColor = #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 0.75)
save(completion: { (true) in
dropOffLocation.setValue("IN TRANZIT", forKey: "btnStatus")
print(dropOffLocation)
})
print(dropOffLocation)
let destinationCoordinate = CLLocationCoordinate2D(latitude: dropOffLocation.latitude, longitude: dropOffLocation.longitude)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinate)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
destinationMapItem.name = dropOffLocation.street
destinationMapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "deliveryAddressCell", for: indexPath) as? AddressCell else { return UITableViewCell() }
let dropOffLocation = dropOffLocations[indexPath.row]
cell.startBtn.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside)
cell.startBtn.titleLabel?.adjustsFontSizeToFitWidth = true
cell.startBtn.titleLabel?.minimumScaleFactor = 0.5
cell.startBtn.titleLabel?.numberOfLines = 1
cell.startBtn.titleLabel?.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
cell.configureCell(dropOffLocation: dropOffLocation)
cell.numberLbl.text = String(indexPath.row + 1)
return cell
}
func save(completion: (_ finished: Bool) -> ()) {
guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
let dropOffLocations = DropOffLocation(context: managedContext)
do {
try managedContext.save()
print("Succesfully saved data!")
completion(true)
} catch {
debugPrint("Could not save: \(error.localizedDescription)")
completion(false)
}
}
您好,刚刚想出如何处理这个问题。首先,我添加了 Bool 类型的核心数据 isINTransit
。在数据模型 init 中有一个 false 值,当我按下按钮时,我将值设置为 true。然后在 cellForRowAt
中进行条件检查,如果为 true,则将按钮标题设置为 IN TRANZIT,如果为 false,则将其设置为 START ROUTE。
我正在尝试根据将由段控件触发的排序标准对单元格重新排序。我在排序时遇到的问题只是对 table 中的标签进行排序,但是文本将根据用户交互更改的按钮等某些元素保持在同一行,这没有意义,应该更多细胞本身。请在点击距离段控件时查看差异 它正确排序数据 问题是按钮 START ROUTE 在第 2 行时被按下并更改为 IN TRANZIT 现在排序它应该在第 1 行但事实并非如此这里的情况。填充 table 的数据来自核心数据。有人能给我指出正确的方向吗?
func sortData() {
if segment.selectedSegmentIndex == 1 {
dropOffLocations.sort(by: {[=11=].dateCreated! < .dateCreated! })
} else if segment.selectedSegmentIndex == 2 {
dropOffLocations.sort(by: {[=11=].distance < .distance })
} else if segment.selectedSegmentIndex == 3 {
dropOffLocations.sort(by: {[=11=].postcode! < .postcode! })
}
tableView.reloadData()
}
@IBAction func segmentChange(_ sender: Any) {
sortData()
}
@objc func tappedButton(sender : UIButton) {
if let cell = sender.superview?.superview?.superview as? UITableViewCell {
if let indexPath = tableView.indexPath(for: cell) {
let dropOffLocation = dropOffLocations[indexPath.row]
sender.setTitle("IN TRANZIT", for: .normal)
sender.titleLabel?.adjustsFontSizeToFitWidth = true
dropOffLocation.btnStatus = sender.titleLabel?.text
sender.backgroundColor = #colorLiteral(red: 0.05882352963, green: 0.180392161, blue: 0.2470588237, alpha: 0.75)
save(completion: { (true) in
dropOffLocation.setValue("IN TRANZIT", forKey: "btnStatus")
print(dropOffLocation)
})
print(dropOffLocation)
let destinationCoordinate = CLLocationCoordinate2D(latitude: dropOffLocation.latitude, longitude: dropOffLocation.longitude)
let destinationPlacemark = MKPlacemark(coordinate: destinationCoordinate)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
destinationMapItem.name = dropOffLocation.street
destinationMapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "deliveryAddressCell", for: indexPath) as? AddressCell else { return UITableViewCell() }
let dropOffLocation = dropOffLocations[indexPath.row]
cell.startBtn.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside)
cell.startBtn.titleLabel?.adjustsFontSizeToFitWidth = true
cell.startBtn.titleLabel?.minimumScaleFactor = 0.5
cell.startBtn.titleLabel?.numberOfLines = 1
cell.startBtn.titleLabel?.transform = CGAffineTransform(rotationAngle: CGFloat.pi / 2)
cell.configureCell(dropOffLocation: dropOffLocation)
cell.numberLbl.text = String(indexPath.row + 1)
return cell
}
func save(completion: (_ finished: Bool) -> ()) {
guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }
let dropOffLocations = DropOffLocation(context: managedContext)
do {
try managedContext.save()
print("Succesfully saved data!")
completion(true)
} catch {
debugPrint("Could not save: \(error.localizedDescription)")
completion(false)
}
}
您好,刚刚想出如何处理这个问题。首先,我添加了 Bool 类型的核心数据 isINTransit
。在数据模型 init 中有一个 false 值,当我按下按钮时,我将值设置为 true。然后在 cellForRowAt
中进行条件检查,如果为 true,则将按钮标题设置为 IN TRANZIT,如果为 false,则将其设置为 START ROUTE。