How to call tableview reloadData from another viewcontroller - swift

I am trying to reload the tableview after UIBarbutton is clicked to save a multi line text in UserDefaults.I want to update the tableview cell which is in another view controller after the button is clicked and load the cell but its not working for me.I tried to add an observer but its not working.The cell will show after reopening the app.What i am missing here?
#objc func saveTapped(){
guard let fact = savedFact else {
return
}
let deviceID = UIDevice.current.identifierForVendor!.uuidString
savedValues.append(fact)
if let arr = UserDefaults.standard.array(forKey: deviceID){
var arrvalues = arr as! [String]
if !arrvalues.contains(fact){
arrvalues.append(fact)
UserDefaults.standard.set(arrvalues, forKey:deviceID)
UserDefaults.standard.synchronize()
print (arr)
}
else
{
UserDefaults.standard.set(savedValues, forKey:deviceID)
}
}
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
}
FavoritesViewController
class FavoritesViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var savedList = [String]()
private func deleteFavorites(text:String){
let deviceID = UIDevice.current.identifierForVendor!.uuidString
if let arr = UserDefaults.standard.array(forKey: deviceID){
var arrvalues = arr as! [String]
if let index = arrvalues.firstIndex(of:text){
arrvalues.remove(at: index)
savedList.remove(at: index)
UserDefaults.standard.set(arrvalues, forKey:deviceID)
UserDefaults.standard.synchronize()
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.separatorStyle = .none
NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
let deviceID = UIDevice.current.identifierForVendor!.uuidString
savedList = UserDefaults.standard.value(forKey: deviceID) as? [String] ?? []
tableView.register(UINib(nibName: "FavoritesTableViewCell", bundle: nil), forCellReuseIdentifier: FavoritesTableViewCell.identifier)
}
#objc func loadList(notification: NSNotification){
//load data here
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
extension FavoritesViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return savedList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: FavoritesTableViewCell.identifier, for: indexPath) as! FavoritesTableViewCell
cell.cellBgImageView.layer.cornerRadius = 9
cell.factsLabel.text = savedList[indexPath.row]
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
deleteFavorites(text: savedList[indexPath.row])
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
}

replace
#objc func loadList(notification: NSNotification){
//load data here
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
as
#objc func loadList(notification: NSNotification){
//load data here
savedList = UserDefaults.standard.value(forKey: deviceID) as? [String] ?? []
DispatchQueue.main.async {
self.tableView.reloadData()
}
}

I know a way, but I don't know if it's the best one.
I always suggest having a shared variable document. In that document, store a variable, which type is FavoritesViewController (in your case).
In FavoritesViewController viewDidLoad(), assign self to the shared variable.
Then, in each other document, subsequently, you can call each method of your FavoritesViewController

Related

I want update the listed data in tableview through swipe trailing

// here is 2 view controller
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {
var arrname = NSMutableArray()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
let dict = arrname[indexPath.row] as? NSMutableDictionary
cell.txtFldName.text = dict!["name"] as? String
cell.txtFldEmail.text = dict!["email"] as? String
cell.txtFldPhone.text = dict!["phone"] as? String
return cell
}
// this swipe editing
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
arrname.removeObject(at: indexPath.row)
tblView.reloadData()
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .normal, title: "edit") { [weak self]
(action, view, completionHandler) in
self?.handlerfun(index: indexPath.row)
completionHandler(true)
}
action.backgroundColor = .systemPink
return UISwipeActionsConfiguration(actions: [action])
}
private func handlerfun(index:Int){
let dict = arrname[index]
NotificationCenter.default.post(
name: NSNotification.Name(rawValue: "data"),
object: dict
)
self.navigationController?.popViewController(animated: true)
}
}
// here is first ViewController from where I need to update
class ViewController: UIViewController,UITextFieldDelegate {
#IBOutlet weak var txtFldName:UITextField!
#IBOutlet weak var txtFldEmail:UITextField!
#IBOutlet weak var txtFldPhone:UITextField!
var namearr = NSMutableArray()
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(
self,
selector: #selector(add),
name: NSNotification.Name(rawValue: "data"),
object: nil
)
}
#objc func add(notification:Notification) {
if let data = notification.object as? NSMutableDictionary {
txtFldName.text = data["name"] as! String
txtFldPhone.text = data["phone"] as! String
txtFldEmail.text = data["email"] as! String
}
}
}
// replace your private func handlerfun with this code :-
private func handleMarkAsEdit(_ index:Int) {
let dict = arrData[index]
let newDict = NSMutableDictionary()
newDict.setValue(dict, forKey: "oldDict")
newDict.setValue(index, forKey: "index")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "pass"), object: newDict)
self.navigationController?.popViewController(animated: true)
}
// add this in second vc after viewDidLoad
if index != -1 {
arrData.replaceObject(at: index, with: dict)
tableVw.reloadData()
}
//ADD this code in first vc after set value in dictionary
if index != -1 {
vc.arrData = self.array
vc.index = self.index
vc.dict = dataDict
index = -1
} else {
array.add(dataDict)
vc.arrData = self.array
}
// replace add function in first vc with this code
#objc func add(notification:Notification){
if let data = notification.object as? NSMutableDictionary {
let oldDict = data["oldDict"] as! NSMutableDictionary
validateNameTxtFld.text = oldDict["name"] as? String ?? ""
validateEmailTxtFld.text = oldDict["email"] as? String ?? ""
validatePhoneTxtFld.text = oldDict["phone"] as? String ?? ""
index = data["index"] as? Int ?? 0
}
}

Swift: UITableViewController selecting cell & passing fetched objects to UIViewController

I’m trying to make a simple note app using Core Data, but I’m running into problem with fetchedResultsController.object(at: indexPath) & passing the data onto my View Controller. using prepare(for segue: UIStoryboardSegue, sender: Any?).
My Core Data Entity is named Notes with the following Attributes
dateStamp Integer 64
noteName String
noteImage Data
noteDescription String
However in order to understand the problem I’ve made separate project & limited it to just the noteName & dateStamp.
So there are three Controllers & one Helper file
MasterViewController
AddViewController
DetailViewController
EditViewController
dateHelper
The MasterView is my UITableController with my NSFetchedResultsControllerDelegate, so the code is as follows….
import UIKit
import CoreData
class MasterViewController: UITableViewController {
private let noteCreationTimeStamp : Int64 = Date().timetoSeconds()
var managedObjectContext: NSManagedObjectContext? {
return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}
var fetchedResultsController: NSFetchedResultsController<Notes>!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
fetchFromCoreData()
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
navigationItem.rightBarButtonItem = addButton
}
#objc
func insertNewObject(_ sender: Any) {
let addController = storyboard?.instantiateViewController(withIdentifier: "addNotes")
as! UINavigationController
self.present(addController, animated: true, completion: nil)
}
private func configureCells(cell: noterViewCell, withEvent note: Notes, indexPath: IndexPath) {
let record = fetchedResultsController.object(at: indexPath)
cell.noteName.text = record.noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: record.dateStamp))
if let noteName = record.value(forKey: "noteName") as? String, let dateTime = record.value(forKey: "dateStamp") as? Int64 {
cell.noteName.text = noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: dateTime))
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return fetchedResultsController.sections?.count ?? 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let sections = self.fetchedResultsController?.sections else {
fatalError("No sections in fetchedResultsController")
}
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "noteCell", for: indexPath) as! noterViewCell
guard let object = self.fetchedResultsController?.object(at: indexPath) else {
fatalError("Attempt to configure cell without a managed object")
}
configureCells(cell: cell, withEvent: object, indexPath: indexPath)
cell.selectbutton.addTarget(self, action: #selector(selectNote(_:)), for: .touchUpInside)
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let context = fetchedResultsController.managedObjectContext
context.delete(fetchedResultsController.object(at: indexPath))
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
func fetchFromCoreData() {
let fetchRequest: NSFetchRequest<Notes> = Notes.fetchRequest()
fetchRequest.fetchBatchSize = 800
let creationDateSortDescriptor = NSSortDescriptor(key: "dateStamp", ascending: false)
//let nameSortDescriptor = NSSortDescriptor(key: "noteName", ascending: false)
fetchRequest.sortDescriptors = [creationDateSortDescriptor]
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: "dateStamp", cacheName: nil)
aFetchedResultsController.delegate = self
fetchedResultsController = aFetchedResultsController
do {
try fetchedResultsController.performFetch()
self.tableView.reloadData()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.localizedDescription), \(nserror.localizedFailureReason ?? "could not retrieve")")
//print("Could not save note to CoreData: \(error.localizedDescription)")
}
}
}
extension MasterViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) {
switch type {
case .insert:
tableView.insertSections(IndexSet(integer: sectionIndex), with: .fade)
case .delete:
tableView.deleteSections(IndexSet(integer: sectionIndex), with: .fade)
default:
return
}
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case .insert:
tableView.insertRows(at: [newIndexPath!], with: .fade)
case .delete:
tableView.deleteRows(at: [indexPath!], with: .fade)
case .update:
tableView.reloadRows(at: [newIndexPath!], with: .fade)
if let updateIndexPath = newIndexPath {
let cell = self.tableView.cellForRow(at: updateIndexPath) as! noterViewCell
let event = anObject as! Notes
cell.dateLabel.text = event.noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: event.dateStamp))
}
case .move:
configureCells(cell: tableView.cellForRow(at: indexPath!) as! noterViewCell, withEvent: anObject as! Notes, indexPath: indexPath!)
tableView.moveRow(at: indexPath!, to: newIndexPath!)
#unknown default:
fatalError("Unresolved error")
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
}
My AddViewController
import UIKit
import CoreData
class AddViewController: UIViewController {
#IBOutlet var noteField: UITextField!
#IBOutlet var dateStamp: UILabel!
private let noteCreationTimeStamp : Int64 = Date().timetoSeconds()
let masterView = MasterViewController()
var isExisting: Bool = false
var note:Notes? = nil
var managedObjectContext: NSManagedObjectContext? {
return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}
override func viewDidLoad() {
super.viewDidLoad()
configureView()
}
var detailItem: Notes? {
didSet {
// Update the view.
configureView()
}
}
func configureView() {
if let detail = detailItem {
if let noteFieldLabel = noteField, let dateTime = dateStamp {
noteFieldLabel.text = detail.noteName
dateTime.text = dateHelper.convertDate(date: Date.init(seconds: detail.dateStamp))
}
}
}
#IBAction func saveImageButtonPressed(_ sender: Any) {
let context = self.managedObjectContext
let newEvent = Notes(context: context!)
newEvent.noteName = noteField.text
newEvent.dateStamp = noteCreationTimeStamp
do {
try context?.save()
} catch {
let error = error as NSError
fatalError("Unresolved error \(error), \(error.localizedDescription)")
}
let isPresentingMode = self.presentingViewController is UINavigationController
if isPresentingMode {
self.dismiss(animated: true, completion: nil)
}
else {
self.navigationController!.pushViewController(masterView, animated: true)
}
}
#IBAction func cancelView(_ sender: AnyObject) {
let isPresentingMode = self.presentingViewController is UINavigationController
if isPresentingMode {
self.dismiss(animated: true, completion: nil)
}
}
}
Everything is good there my saveImageButtonPressed saves the data to my managedObjectContext and displays onto the UITableview with no problems.
This code for my DetailViewController
import UIKit
import CoreData
class DetailViewController: UIViewController {
#IBOutlet var dateLabel: UILabel!
#IBOutlet var noteField: UILabel!
let masterView = MasterViewController()
var notes: Notes?
var myNotes = [Notes]()
var isExisting: Bool = false
var index = IndexPath()
private let noteCreationTimeStamp : Int64 = Date().timetoSeconds()
var managedObjectContext: NSManagedObjectContext? {
return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}
var detailItem: Notes? {
didSet {
// Update the view.
configureView()
}
}
func configureView() {
if let detail = detailItem {
if let noteFieldLabel = noteField, let dateStamp = dateLabel {
noteFieldLabel.text = detail.noteName
dateStamp.text = dateHelper.convertDate(date: Date.init(seconds: detail.dateStamp))
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
//fetchNote()
configureView()
}
#IBAction func cancelView(_ sender: AnyObject) {
let isPresentingMode = self.presentingViewController is UINavigationController
if isPresentingMode {
self.dismiss(animated: true, completion: nil)
}
}
//
func fetchNote() {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Notes")
do {
myNotes = try managedObjectContext?.fetch(fetchRequest) as! [Notes]
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.localizedDescription,\(nserror.localizedFailureReason ?? "could not retrieve")")
}
}
//
}
So I’m trying to select my cell to pass the saved data to my DetailViewController but it is not selecting. Nothing happens. The fetchNote function in my DetailViewController was just add-on to try out.
This is my code for my prepare(for segue: UIStoryboardSegue, sender: Any?)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
if let selectedIndexPath = tableView.indexPathForSelectedRow {
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
let objects = fetchedResultsController.object(at: selectedIndexPath)
controller.detailItem = objects
}
}
}
and my tableView.didSelectRowAt: indexPath
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let cell = self.tableView.cellForRow(at: indexPath) as! noterViewCell
let event = fetchedResultsController.object(at: indexPath)
cell.dateLabel.text = event.noteName
cell.dateLabel.text = dateHelper.convertDate(date: Date.init(seconds: event.dateStamp))
//configureCells(cell: tableView.cellForRow(at: indexPath!) as! noterViewCell, withEvent: anObject as! Notes, indexPath: indexPath!)
self.performSegue(withIdentifier: "showDetails", sender: self)
}
I added self.performSegue(withIdentifier: "showDetails", sender: self) but the DetailViewController is just blank.
In order to understand further I added button in the cellForRowAt indexPath:…
cell.selectbutton.addTarget(self, action: #selector(selectNote(_:)), for: .touchUpInside)
with the following action
#objc
func selectNote(_ sender: noterViewCell) {
let indexPath = IndexPath()
let objects = fetchedResultsController.object(at: indexPath) as Notes
let controller = DetailViewController()
controller.detailItem = objects
//let detailView = //storyboard?.instantiateViewController(withIdentifier: "viewNotes") as! //UINavigationController
//self.present(detailView, animated: true, completion: nil)
let detailViews = DetailViewController()
self.navigationController?.popToViewController(detailViews, animated: true)
}
and this threw an 'NSInvalidArgumentException', reason: 'no section at index 9223372036854775807' with both methods tried in accessing my Detail View
This was directed exactly on fetchedResultsController.object in my button action #objc MasterViewController_selectNote(_:)
Also listed in the thread was [NSFetchedResultsController objectAtIndexPath:]: scrolling down I found another reason, "cannot access fetched objects before -performFetch:" as well as "NSFetchedResultsController: no object at index %lu in section at index %lu"
I’m calling my fetchFromCoreData() in my viewDidLoad() … I took out the performFetch I pasted it directly in the viewDidLoad(). Still throws the same error if I click on the button. Again blank view selecting using prepareForSegue. I tried using return value in my fetchFromCoreData() function again the same.
I’m not sure if I added to the problem by adding the button but in all cases I’ve attempted to remedy the problem prepareForSegue its still passing a blank view.
I also tried to validate the indexPath using this function
func validateIndexPath(_ indexPath: IndexPath) -> Bool {
if let sections = self.fetchedResultsController.sections,
indexPath.section < sections.count {
if indexPath.row < sections[indexPath.section].numberOfObjects {
return true
}
}
return false
}
again to no avail, calling it on the cellForRowAt indexPath & the app still terminates with the same 'NSInvalidArgumentException’ and the prepareForSegue passing blank view.
Then I added my titleForHeaderInSection in my tableView which is set by month & year that is displaying okay when I save the data in my AddViewController. I hoped it might make a bit of difference but…
func monthDate(string:String?) -> String? {
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM"
let dateString = formatter.string(from: date)
let monthDate = formatter.date(from: dateString)
formatter.dateFormat = "MMM, yyyy"
let dateMonth = formatter.string(from: monthDate!)
return dateMonth
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let sectionInfo = fetchedResultsController.sections?[section] else {
return nil
}
return monthDate(string: sectionInfo.name)
}
I really just don't understand where I'm going wrong... I’m using iPhone 11 pro max simulator on IOS14 & Xcode 12.3.(I upgraded about month ago & it did hang on install for about 4 or 5 days, its been bit buggy since)
Although I’m sure I’ve probably missed something quite simple any help will be much appreciated.
On the other hand if I've understood correctly I’m aware NSFetchedResultsController does not like empty sections as per this blog post…
http://www.iosnomad.com/blog/2014/8/6/swift-nsfetchedresultscontroller-trickery
and if that could be the problem, is there simpler solution for Swift 5?
EDIT
In my tableview didSelectRowAt I added
print(fetchedResultsController.object(at: indexPath))
and the console outputs my saved data but it's still not passing it to my DetailViewController.
I tried also in my prepareForSegue adding
print(fetchedResultsController.object(at: selectedIndexPath))
This returned no saved data, as well as returning 0 sections after printing out on the console the numberOfSections in tableView, after selecting my cell. But as I open my app the data is still saved and viewable in my cell and prints out correct section numbers in the console, as I add new note. Only when I select my cell to pass saved data to my DetailViewController the problem occurs.
Problem Solved
I added following variables to my UITableViewController
var cellLabel: [Notes] = []
var selectedLabels = String()
var selectedTime = String()
and added variables to my DetailViewController
var receivedString: String = ""
var recievedTime: String = ""
I changed my code to reflect fetched objects are attached to my selected variables in my didSelectRowAt and equal to my received variables in my prepareForSegue.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let event = fetchedResultsController.object(at: indexPath)
cellLabel = [event]
selectedLabels = event.noteName!
selectedTime = dateHelper.convertDate(date: Date.init(seconds: event.dateStamp))
print(selectedLabels)
print(selectedTime)
self.performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetails" {
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.receivedString = selectedLabels
controller.recievedTime = selectedTime
print(selectedLabels)
print(selectedTime)
/*
if let indexPath = self.tableView.indexPathForSelectedRow {
let objects = fetchedResultsController.object(at: indexPath)
controller.noteField.text = objects.noteName
controller.detailItem = objects
}
*/
}
}
and in my viewDidLoad() in DetailViewController
override func viewDidLoad() {
super.viewDidLoad()
noteField.text = receivedString
dateLabel.text = recievedTime
}

Getting user input to copy from a TableViewController to a ViewController using Swift

I'm working on an app in which the user inputs three values (artist, album and release date). I'm trying to get those user entered values to 'copy' over to another TableViewController.
The values show on this, the FreshReleaseTableViewController (the code below) and I need them to copy to EditFreshReleaseViewController.
How would I go about this?
import UIKit
import CoreData
import UserNotifications
class FreshReleaseTableViewController: UITableViewController{
var freshreleases = [Release_Date]()
let dateFormatter = DateFormatter()
override func viewDidLoad() {
super.viewDidLoad()
//create a new button
let button = UIButton.init(type: .custom)
//set image for button
button.setImage(UIImage(named: "Mic App Logo.png"), for: UIControlState.normal)
dateFormatter.dateStyle = .full
dateFormatter.timeStyle = .none
}
#objc func editAction() {
let viewController = AddfreshreleaseViewController()
navigationController?.present(viewController, animated: true, completion: nil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetchRequest = Release_Date.fetchRequest() as NSFetchRequest<Release_Date>
let sortDescriptor1 = NSSortDescriptor(key: "artist", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "album", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2]
do {
freshreleases = try context.fetch(fetchRequest)
} catch let error {
print("Could not fetch because of error: \(error).")
}
/*let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date > %#", startOfToday)
fetchRequest.predicate = predicate*/
NSFetchedResultsController<NSFetchRequestResult>()
fetchRequest.predicate = NSPredicate(format: "isReleased = NO")
tableView.reloadData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return freshreleases.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FreshReleaseCellIdentifier", for: indexPath)
let freshrelease = freshreleases[indexPath.row]
cell.textLabel?.numberOfLines = 0
let artist = freshrelease.artist ?? ""
let album = freshrelease.album ?? ""
cell.textLabel?.text = artist + "'s\nnew album '" + album + "'\nreleases"
if let date = freshrelease.release_date as Date? {
cell.detailTextLabel?.text = dateFormatter.string(from: date)
} else {
cell.detailTextLabel?.text = ""
}
return cell
}
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if freshreleases.count > indexPath.row {
let freshrelease = freshreleases[indexPath.row]
// Remove notification
if let identifier = freshrelease.release_dateId {
let center = UNUserNotificationCenter.current()
center.removePendingNotificationRequests(withIdentifiers: [identifier])
}
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
context.delete(freshrelease)
freshreleases.remove(at: indexPath.row)
do {
try context.save()
} catch let error {
print("Could not save \(error)")
}
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
#available(iOS 11.0, *)
override func tableView(_ tableView: UITableView,
leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
let modifyAction = UIContextualAction(style: .normal, title: "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
let MainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc : UIViewController = MainStoryboard.instantiateViewController(withIdentifier: "FreshReleaseEdit") as UIViewController
self.present(vc, animated: true, completion: nil)
success(true)
})
modifyAction.title = "Edit"
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
}
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
}
Add to your EditFreshReleaseViewController variable which represents selected fresh release
var selectedFreshRelease: Release_Date?
now in line where you're declaring view controller which is gonna be presented, downcast this controller as EditFreshReleaseViewController
let vc = MainStoryboard.instantiateViewController(withIdentifier: "FreshReleaseEdit") as! EditFreshReleaseViewController
then just assign its variable as item from freshReleases array on index indexPath.row
vc.selectedFreshRelease = self.freshReleases[indexPath.row]
finally, present this view controller as you do
self.present(vc, animated: true, completion: nil)

UITableView don't reload data

I got an array that populates a tableview, it works fine when I run the app.
I created a popover with a PickerView to choose one option to sort the TableView data.
I get the user choise in the popover, pass it to the main ViewController, sorted the data and called tableview.reloadData() but nothing happens.
I printed the array after the sort and the array is sorted but I can't saw the changes.
But if I go to other ViewController and came back the data is changed.
Why the changes are not showing when I call the tableview.reloadData().
Here's the code:
var dataModel = DataModel()
var ordenacao = String()
override func viewWillAppear(_ animated: Bool) {
dataModel.loadData()
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
ordenadados(ordem: ordenacao)
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "cellIdentifier")
cell.textLabel?.text = dataModel.notas[indexPath.row].titulo
cell.detailTextLabel?.text = dataModel.notas[indexPath.row].datafinal
print("Ordena Tableview")
for nota in dataModel.notas {
print (nota.titulo ?? "")
}
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataModel.notas.count
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
dataModel.notas.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)
dataModel.saveData()
}
EDIT:
func ordenadados(ordem: String){
dataModel.loadData()
if(ordenacao == "Titulo Asc."){
print("Titulo Asc")
dataModel.notas.sort { $0.titulo! < $1.titulo! }
}else if(ordenacao == "Titulo Desc."){
print("Titulo Desc.")
dataModel.notas.sort { $0.titulo! > $1.titulo! }
}
dataModel.saveData()
for nota in dataModel.notas {
print (nota.titulo ?? "")
}
dataModel.loadData()
tableView.reloadData()
}
In the output the array was sorted but in the TableView nothing changed.
Save and Load Data methods:
//save data
func saveData() {
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encode(notas, forKey: "teste")
archiver.finishEncoding()
data.write(toFile: dataFilePath(), atomically: true)
}
//read data
func loadData() {
let path = self.dataFilePath()
let defaultManager = FileManager()
if defaultManager.fileExists(atPath: path) {
let url = URL(fileURLWithPath: path)
let data = try! Data(contentsOf: url)
let unarchiver = NSKeyedUnarchiver(forReadingWith: data)
notas = unarchiver.decodeObject(forKey: "teste") as! Array
unarchiver.finishDecoding()
}
}

searchBar filtered tableViewCells didSelectItemAt indexPath display in navigationBar Title

enter image description hereI am having trouble implementing a didSelectRowAtIndexPath from a searchBar filtered tableView cell. When I update the tableView list based on searchbar textDidChange, and perform a show segue to another ViewController, the navigationBar Title is always displaying the non-filtered tableViews indexPath 0. In other words, I would like to have the navigation title display the text from the didSelectAtIndex of search results tableView (not the original indexPath cell text from the non-filtered tableView). Hopefully that makes sense, and thanks in advance!
// viewDidLoad method
override func viewDidLoad() {
super.viewDidLoad()
searchBar.searchBarStyle = UISearchBarStyle.prominent
searchBar.placeholder = " Search Places..."
searchBar.sizeToFit()
searchBar.isTranslucent = false
searchBar.backgroundImage = UIImage()
searchBar.delegate = self
searchBar.returnKeyType = UIReturnKeyType.done
navigationItem.titleView = searchBar
ref = FIRDatabase.database().reference()
fetchPlaces()
placesClient = GMSPlacesClient.shared()
locationManager.requestAlwaysAuthorization()
tableView.allowsMultipleSelectionDuringEditing = true
}
var placeList = [Place]()
var placesDictionary = [String: Place]()
// fetch places for tableView method
func fetchPlaces() {
let uid = FIRAuth.auth()?.currentUser?.uid
let ref = FIRDatabase.database().reference().child("users").child(uid!).child("Places")
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let place = Place()
place.setValuesForKeys(dictionary)
if let addedBy = place.addedBy {
self.placesDictionary[addedBy] = place
self.placeList.insert(place, at: 0)
}
//this will crash because of background thread, so lets call this on dispatch_async main thread
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
}, withCancel: nil)
}
// search variables
lazy var searchBar:UISearchBar = UISearchBar()
var isSearching = false
var filteredData = [Place]()
// searchBar method
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
isSearching = false
view.endEditing(true)
tableView.reloadData()
} else {
isSearching = true
filteredData = placeList.filter({$0.place?.range(of: searchBar.text!) != nil})
tableView.reloadData()
}
}
// tableView methods
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return filteredData.count
}
return placeList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellId)
if isSearching {
cell.textLabel?.text = filteredData[indexPath.row].place
} else {
cell.textLabel?.text = placeList[indexPath.row].place
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let placeDetailsVC = CurrentUserPlaceDetailsVC()
if isSearching == false {
placeDetailsVC.navigationTitle = placeList[(indexPath.row)].place
show(placeDetailsVC, sender: self)
} else {
placeDetailsVC.navigationTitle = filteredData[(indexPath.row)].place
show(placeDetailsVC, sender: self)
}
}
}
Create a string in your up coming ViewController.
class CurrentUserPlaceDetailsVC: UIViewController {
var navigationTitle: String?
override func viewDidLoad(){
super.viewDidLoad()
self.navigationItem.title = navigationTitle
}
}
Now instead of assigning the title directly to navigationBar you should assign it first to that string and then to navigationBar in viewDidLoad method of your viewController.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let placeDetailsVC = CurrentUserPlaceDetailsVC()
// Get Cell Label
placeDetailsVC.navigationTitle = placeList[indexPath.row].place
show(placeDetailsVC, sender: self)
}