【发布时间】:2018-01-14 11:03:35
【问题描述】:
我正在尝试根据将从段控件触发的排序标准对单元格重新排序。我在对其进行排序时遇到的问题只是对表格中的标签进行排序,但某些元素(如文本将根据用户交互而更改的按钮)停留在同一行,这没有意义,应该更多地与单元格本身有关。请在点击距离段控件时查看差异它对数据进行正确排序问题是按钮 START ROUTE 在第 2 行时被按下并更改为 IN TRANZIT 现在已排序它应该在第 1 行但不是这里的情况。填充表的数据来自核心数据。请有人指出我正确的方向吗?
func sortData() {
if segment.selectedSegmentIndex == 1 {
dropOffLocations.sort(by: {$0.dateCreated! < $1.dateCreated! })
} else if segment.selectedSegmentIndex == 2 {
dropOffLocations.sort(by: {$0.distance < $1.distance })
} else if segment.selectedSegmentIndex == 3 {
dropOffLocations.sort(by: {$0.postcode! < $1.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)
}
}
【问题讨论】:
-
你能发布你的tableView
cellForRowAt:方法吗?我猜您需要在数据模型中存储按钮的状态(“开始路线”v.“运输中”),并使用模型数据在 cellForRowAt 中正确设置它们: -
@pbasdf 嗨,我已经添加了代码的缺失部分。
-
是的 - 您需要跟踪每个 dropOffLocation 的按钮状态。您可以通过向模型添加另一个属性(可能称为
status?)来做到这一点。然后在tappedButton可以改变相关dropOffLocation的状态(或者直接修改按钮标题,或者重新加载对应的单元格)。并且在 cellForRowAt 中,一定要检查状态并相应地设置按钮的标题。 -
@pbasdf 到目前为止,感谢您的帮助检查并再次设置标题?还有一个问题是我需要在模型中保存按钮颜色的参考,因为它会变成更深的颜色?谢谢
-
当你调用 reloadData 时,tableView 会丢弃当前的单元格(将它们放回队列中),然后调用它的数据源方法(例如 cellForRowAt)来重建单元格。 cellForRowAt 代码从队列中取出一个单元并将其配置为反映正确的数据。您需要再次设置按钮标题,否则它将是单元格被放回队列之前的任何内容。无需保留颜色细节:只需根据CoreData中的按钮状态更新代码中的颜色即可。
标签: uitableview sorting core-data swift3 swift4