如何以编程方式重新排序基于排序标准的单元格SWIFT 4

时间:2018-01-14 11:03:36

标签: uitableview sorting core-data swift3 swift4

我试图根据将从细分控件触发的排序标准对细胞进行重新排序。我在排序时遇到的问题只是对表格中的标签进行排序,但是一些元素(如文本将根据用户交互而改变的按钮)保持在同一行,这对于单元本身应该更有意义。当点击距离段控件时,请看到差异,它正确地对数据进行排序问题是按钮START ROUTE在第2行时被按下并且变为IN TRANZIT并且现在已经排序它应该在第1行但是不是这里的情况。填充表的数据来自核心数据。有人能指出我正确的方向吗?

enter image description here enter image description here

                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)
            }
        }

1 个答案:

答案 0 :(得分:0)

你刚刚想出如何处理这件事。首先,我添加了Bool类型的核心数据isINTransit。在数据模型init中使用false值,当我按下按钮时,我将值设置为true。然后在cellForRowAt中进行条件检查if是否为true,然后将按钮标题设置为IN TRANZIT,如果为false则将其设置为START ROUTE。