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

问题描述 投票:0回答:2

我有3个用户输入字段,每个字段创建一个不同的数组。这些数组用于填充UITableViewCell。我的目的是按日期对添加的单元格进行排序。

其中一个输入字段包含一个日期,该日期将用于对此表进行排序。要显示日期,数组的类型为String,因此日期格式为String

假设我不会直接将它格式化为String并在我之前对其进行排序。有没有办法根据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)
        }
    }

}

编辑:

好的,我添加了一个struct和一个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 })
    }

现在在TableViewController中:我如何设法从数组内部的一个结构中获取数据并将其添加到单元格中,就像我之前使用我的数组一样?

ios swift
2个回答
3
投票

正确的解决方案是拥有一个阵列,而不是四个阵列。创建一个包含4个属性的struct。然后有一个该结构的数组。然后,根据需要添加,删除,排序和过滤数组以填充表视图变得微不足道。


2
投票

相反,使用所有这些数组,您可以考虑使用包含所有这些数组的其他类型,例如:

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

然后你会有一个foos数组,你可以按日期排序。这样一切都将被排序,而无需处理不同的数组。

© www.soinside.com 2019 - 2024. All rights reserved.