【问题标题】:Swift - Sort different arrays accordingly to date arraySwift - 根据日期数组对不同的数组进行排序
【发布时间】:2019-04-29 19:51:53
【问题描述】:

我有 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 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 中:我如何设法从数组内部的一个结构中获取数据并将其添加到单元格中,就像我之前对数组所做的那样?

【问题讨论】:

  • 你的代码没有意义。 dateNotificationArray 是什么?如果dateArray是日期数组,为什么是String数组?
  • Rob Napier 的这段视频在 7 点 14 分正是讨论了您的问题。 youtu.be/_S6UOrwS-Tg?t=434
  • @matt 我之前没有想过对数组进行排序,所以我在用户输入后立即将 Date 转换为 String 以显示它。

标签: ios swift


【解决方案1】:

正确的解决方案是使用一个数组,而不是四个。创建一个具有 4 个属性的 struct。然后有一个该结构的数组。然后根据需要添加、删除、排序和过滤数组以填充表格视图就变得很简单了。

【讨论】:

  • 谢谢。我已将我的进度添加到我最初的问题中。我如何设法从数组内部的一个结构中获取数据并将其添加到单元格中,就像我之前对数组所做的那样?
  • 没有什么不同。 let rowData = notStructArray[indexPath.row]。然后访问rowData 的属性来设置单元格,就像你正在做的那样。 cell.setTitle(title: rowData.title)
  • 出现如下错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object ( "Fuel_Check.not(title: \"T\U00dcV\", desc: \"N\U00e4chster T\U00dcV Termin\", date: 2019-05-01 18:50:00 +0000, type: 1)" ) for key notStructArray'
  • 这是一个全新的问题。对该错误进行一些研究。如果您在新问题上需要更多帮助,请发布包含所有相关详细信息的新问题。
【解决方案2】:

您可以考虑使用包含所有这些数组的另一种类型,而不是使用所有这些数组,例如:

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

然后你会得到一个foos 的数组,你可以按日期排序。这样一来,无需处理不同的数组即可对所有内容进行排序。

【讨论】:

  • 非常感谢您的帮助。我已将我的进度添加到我最初的问题中。我如何设法从数组内部的一个结构中获取数据并将其添加到单元格中,就像我之前对数组所做的那样?
  • 只需在行的单元格中执行此操作:let not = notStructArray[indexPath.row] 并使用 not 填充视图:cell.setTitle(not.title)
猜你喜欢
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
相关资源
最近更新 更多