我正在嘗試根據日期過濾通知。因此,我將在 tableview 的每個單獨部分中顯示它們。
到目前為止,我能夠獲取日期并將日期與今天、上個月或更早的日期進行比較,如下所示。
var todaySectionNotificationsArray = [NewNotificationModel]()
var lastMonthSectionNotificationsArray = [NewNotificationModel]()
var oldSectionNotificationsArray = [NewNotificationModel]()
let now = Date()
let today = Calendar.current.date(byAdding: .day, value: -1, to: Date())!
let lastMonth = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
var old = Calendar.current.date(byAdding: .month, value: -2, to: Date())!
我為他們創建了 3 個空陣列,如果他們的日期是我正在尋找的,我會嘗試附加元素。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch cellTypes[indexPath.row] {
case .notification:
if (notificationsViewModel[indexPath.row].toDate()! < today) {
todaySectionNotificationsArray.append(notificationsViewModel[indexPath.row])
} else if (notificationsViewModel[indexPath.row].toDate()! < lastMonth) {
lastMonthSectionNotificationsArray.append(contentsOf:notificationsViewModel[indexPath.row])
} else if (notificationsViewModel[indexPath.row].toDate()! > lastMonth) {
oldSectionNotificationsArray = notificationsViewModel[indexPath.row])
}
return configureNotificationCell(indexPath: indexPath)
case .dummy: return configureDummyCell(indexPath: indexPath)
case .empty: return configureEmptyCell(indexPath: indexPath)
case .error: return configureErrorCell(indexPath: indexPath)
}
}
但是那些.append給了我錯誤 “在呼叫實體方法'append'時沒有完全匹配”并且 =運算子給出了“運算子函式' ='要求'NewNotificationViewModel'符合'Sequence'”的錯誤
這是我的 NewNotificationViewModel
final class NewNotificationViewModel {
// MARK: Properties
var notificationModel: NewNotificationModel!
private(set) var id: String?
private(set) var fullName: String!
private(set) var firstLetterName: String!
private(set) var profileImageString: String?
private(set) var profileImageURL: URL?
private(set) var date: String?
private(set) var isRead: Bool!
private(set) var isActionButtonActive: Bool?
private(set) var groupName: String?
private(set) var type: Int?
private(set) var itemDescription: String?
private(set) var itemId: String?
private(set) var mail: String?
private(set) var userId: String!
private(set) var dateTimeStamp: Double!
private(set) var interactionCount: String?
// MARK: Initialization
init(notificationModel: NewNotificationModel) {
self.notificationModel = notificationModel
self.id = notificationModel.id
self.fullName = getFullName()
self.firstLetterName = getFirstLetters()
self.profileImageURL = getProfileImageURL()
self.date = getDate()
self.isRead = notificationModel.isRead
self.isActionButtonActive = notificationModel.isActionButtonActive
self.groupName = notificationModel.groupName
self.type = notificationModel.type
self.itemDescription = notificationModel.item?.description
self.itemId = notificationModel.item?.itemId
self.mail = getMail()
self.userId = getUserId()
self.dateTimeStamp = getTimeStamp()
self.interactionCount = notificationModel.interactionCount
}
}
extension NewNotificationViewModel {
func toDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: self)
}
這是 NewNotificationModel
class NewNotificationModel: Serializable {
var id: String?
var fromUser: NewNotificationFromUserModel!
var type: Int?
var item: NewNotificationItemModel?
var isRead: Bool?
var creationTime: String?
var isActionButtonActive: Bool?
var groupName: String?
var interactionCount: String?
}
所以現在我不知道該怎么辦。即使想出這個解決方案,我也花了 3-4 個小時。
uj5u.com熱心網友回復:
根據您的模型和 viewModel,我認為您可以將 viewModel 的屬性(notificationModel)附加到陣列中,如下所示
todaySectionNotificationsArray.append(notificationsViewModel[indexPath.row].notificationModel)
uj5u.com熱心網友回復:
看起來您不小心傳入了toDate() 函式上self的屬性而不是date屬性NewNotificationViewModel
func toDate() -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
return dateFormatter.date(from: self)
}
這一行:return dateFormatter.date(from: self)應該是return dateFormatter.date(from: date ?? "")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440614.html
