我有 3 个用户输入字段,每个字段创建一个不同的数组。这些数组用于填充UITableViewCell
. 我的目的是按日期对添加的单元格进行排序。
其中一个输入字段包含一个日期,该日期将用于对该表进行排序。为了同时显示日期,数组的类型为String
,因此日期格式为String
。
假设我不会String
在我做之前直接将其格式化和排序。有没有办法根据日期数组对其他数组进行排序?
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 中:我如何设法从数组内部的一个结构中获取数据并将其添加到单元格中,就像我之前使用数组所做的那样?
正确的解决方案是拥有一个阵列,而不是四个。创建一个struct
具有 4 个属性的。然后有一个该结构的数组。然后根据需要添加、删除、排序和过滤数组以填充表视图变得微不足道。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句