Я что-то здесь упускаю.Я пытаюсь построить tableviewcontroller, который будет отображать все ожидающие уведомления пользователя в моем приложении.
Вот код:
import UIKit
import UserNotifications
class NotificationsPendingTVC: UITableViewController {
struct SANotifications {
var title: String
var subtitle: String
var trigger: UNNotificationTrigger?
}
let reuseIdentifier = "cellID"
var pendingNotifications: [SANotifications]!
//--------------------------------------------------------------------------------
// Search Controller
//--------------------------------------------------------------------------------
var filteredNotifications: [SANotifications]!
var resultSearchController = UISearchController()
//--------------------------------------------------------------------------------
override func viewDidLoad() {
super.viewDidLoad()
filteredNotifications = [SANotifications]()
pendingNotifications = [SANotifications]()
resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.hidesNavigationBarDuringPresentation = false
controller.searchBar.sizeToFit()
tableView.tableHeaderView = controller.searchBar
return controller
})()
/*--------------------------------------------------------------------------------*/
// Refresh
/*--------------------------------------------------------------------------------*/
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshAction), for: UIControlEvents.valueChanged)
self.refreshControl = refreshControl
// Reload the table
tableView.reloadData()
}
@objc func refreshAction() {
refreshControl?.beginRefreshing()
loadNotificationsFromSystem()
}
override func viewWillAppear(_ animated: Bool) {
loadNotificationsFromSystem()
super.viewWillAppear(animated)
}
override func viewWillDisappear(_ animated: Bool) {
self.pendingNotifications.removeAll()
self.filteredNotifications.removeAll()
super.viewWillAppear(animated)
}
/*--------------------------------------------------------------------------------*/
//MARK: - Private Functions
/*--------------------------------------------------------------------------------*/
func loadNotificationsFromSystem() {
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(completionHandler: { [weak self] (notificationsRequests) in
self?.pendingNotifications.removeAll()
self?.filteredNotifications.removeAll()
for notificationsRequest in notificationsRequests {
let notificationRqst = SANotifications(title: notificationsRequest.content.title,
subtitle: notificationsRequest.content.subtitle,
trigger: notificationsRequest.trigger)
self?.pendingNotifications.append(notificationRqst)
self?.filteredNotifications.append(notificationRqst)
}
DispatchQueue.main.async { [weak self] in
self?.refreshControl?.endRefreshing()
self?.tableView.reloadData()
}
})
}
/*--------------------------------------------------------------------------------*/
//MARK: - Table view data source
/*--------------------------------------------------------------------------------*/
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if resultSearchController.isActive {
return filteredNotifications.count
} else {
return pendingNotifications.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
let row = indexPath.row
let notification: SANotifications!
if resultSearchController.isActive {
notification = filteredNotifications[row]
} else {
notification = pendingNotifications[row]
}
// Configure the cell...
cell.textLabel?.text = "\(notification.title)"
cell.detailTextLabel?.text = "\(notification.subtitle)"
return cell
}
/*--------------------------------------------------------------------------------*/
//MARK: - Table View Delegate
/*--------------------------------------------------------------------------------*/
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
return nil
}
}
/*--------------------------------------------------------------------------------*/
//MARK: - UISearchResultsUpdating
/*--------------------------------------------------------------------------------*/
extension NotificationsPendingTVC : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text?.lowercased() else { return }
if searchText == "" {
filteredNotifications = pendingNotifications
} else {
filteredNotifications.removeAll(keepingCapacity: false)
let results = pendingNotifications.filter{$0.title.lowercased().contains(searchText) || $0.subtitle.lowercased().contains(searchText)}
filteredNotifications = results
}
self.tableView.reloadData()
if let parentCtl = self.parent as? NotificationsTabBarCtl {
let count = self.filteredNotifications.count
parentCtl.updateTitle(notificationsCount: count)
}
}
}
Когда я запускаю его, захожу в представление таблицы и захожувернуться к основному (главному) контроллеру представления. Контроллер представления таблицы сохраняется следующим образом:
Есть какие-либо предположения о том, что я делаю здесь неправильно?
Заранее спасибо
Роб