简体   繁体   English

Swift-根据日期数组对不同数组进行排序

[英]Swift - Sort different arrays accordingly to date array

I have 3 user input fields which each create a different array. 我有3个用户输入字段,每个字段创建一个不同的数组。 These arrays are being used to fill a UITableViewCell . 这些数组用于填充UITableViewCell My intention is to sort the added cells by date. 我的意图是按日期对添加的单元格进行排序。

One of the input fields contains a date which shall be used to sort this table. 输入字段之一包含一个日期,该日期将用于对该表进行排序。 To also display the date, the array is of type String , hence the dates are formatted to String . 为了同时显示日期,数组的类型为String ,因此日期被格式化为String

Let's say I would not directly format it to String and sort it before I do. 假设我不会直接将其格式化为String并对其进行排序。 Is there a way to sort the other arrays according to the Date array? 有没有一种方法可以根据Date数组对其他数组进行排序?

import UIKit

class NotificationViewController: UIViewController {

    let defaults = UserDefaults.standard

    @IBOutlet weak var toolbar: UIToolbar!
    @IBOutlet weak var notificationView: UITableView!

    var isVisible = false

    var descArray: [String] = []
    var titleArray: [String] = []
    var dateArray: [String] = []
    var typeArray: [Int] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        declareArrays()
        print(dateArray)
        self.toolbar.setBackgroundImage(UIImage(),
                                        forToolbarPosition: .any,
                                        barMetrics: .default)
        self.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
        notificationView.delegate = self
        notificationView.dataSource = self
    }

    func declareArrays() {
        descArray = getDesc()
        titleArray = getTitle()
        dateArray = getDate()
        typeArray = getType()
    }

    func getDesc() -> [String] {
        return descNotificationArray
    }

    func getTitle() -> [String] {
        return titleNotificationArray
    }

    func getDate() -> [String] {
        return dateNotificationArray
    }

    func getType() -> [Int] {
        return notificationTypeArray
    }

    @IBAction func goBack(_ sender: Any) {
        performSegue(withIdentifier: "goBackToMain", sender: self)
    }

    @IBAction func addNotification(_ sender: Any) {
       performSegue(withIdentifier: "goToType", sender: self)
    }

}

extension NotificationViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dateArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let date = dateArray[indexPath.row]
        let title = titleArray[indexPath.row]
        let desc = descArray[indexPath.row]
        let notType = typeArray[indexPath.row]

        if notType == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TuvTableViewCell") as! TuvTableViewCell
            cell.setTitle(title: title)
            cell.setDesc(desc: desc)
            cell.setDate(date: date)
            return cell
        } else if notType == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ServiceNotTableViewCell") as! ServiceNotTableViewCell
            cell.setTitle(title: title)
            cell.setDesc(desc: desc)
            cell.setDate(date: date)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationViewCell") as! NotificationViewCell
            cell.setTitle(title: title)
            cell.setDesc(desc: desc)
            cell.setDate(date: date)
            return cell
        }
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == .delete) {
            dateArray.remove(at: indexPath.item)
            titleArray.remove(at: indexPath.item)
            descArray.remove(at: indexPath.item)

            descNotificationArray.remove(at: indexPath.item)
            dateNotificationArray.remove(at: indexPath.item)
            titleNotificationArray.remove(at: indexPath.item)

            defaults.set(descNotificationArray, forKey: "descNotificationArray")
            defaults.set(dateNotificationArray, forKey: "dateNotificationArray")
            defaults.set(titleNotificationArray, forKey: "titleNotificationArray")

            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }

}

Edit: 编辑:

Ok, I've added an struct and a func which appends the input to the array and sorts it: 好的,我添加了一个结构和一个func,它将输入添加到数组并对其进行排序:

struct not {
    var title: String
    var desc: String
    var Date: Date
}

func appendToStructArray() {
        let notificationData: not = not(title: notifTitle.text!, desc: notifDescribtion.text!, Date: datePicker.date)
        notStructArray.append(notificationData)
        notStructArray.sort(by: { $0.Date < $1.Date })
    }

Now in the TableViewController: How do I manage to get the data from one struct inside of the array and add it to a cell just like I did it previously with my arrays? 现在在TableViewController中:如何像从前对数组所做的那样,如何从数组中的一个结构获取数据并将其添加到单元格中?

The proper solution is to have one array, not four. 正确的解决方案是使用一个数组,而不是四个。 Create a struct with 4 properties. 创建具有4个属性的struct Then have one array of that struct. 然后拥有该结构的一个数组。 Then it becomes trivial to add, remove, sort, and filter the array as needed to populate your table view. 然后,根据需要添加,删除,排序和过滤数组以填充表视图就变得很简单。

Instead using all those arrays, you might consider using another type that comprises all of those, something like: 除了使用所有这些数组,您还可以考虑使用包含所有这些数组的另一种类型,例如:

struct Foo {
  let date: Date
  let desc: String
  let type: String
  let title: String
}

Then you would have an array of foos that you could sort by date. 然后,您将获得可以按日期排序的foos数组。 That way everything would be sorted without the need of handling different arrays. 这样,无需处理不同的数组即可对所有内容进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM