Getting data to transfer from one TableViewController to another once a date passes
up vote
0
down vote
favorite
I'm working on an app which is date based. I'm trying to get information to pass from one TableViewController to another.
It's a notification based app that reminds the user that something comes out today. So on the date it comes out, I'd like it to come off the release list and go onto the released list (So if something comes out the Nov.8th, on that date it'll be moved to the released list).
I'm having trouble getting it to work. I used the below codes and the date comes and goes without anything happening, it still shows it on the release list.
Below is the code I used on the release list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date > %@", startOfToday)
fetchRequest.predicate = predicate
And here's the code on the released list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date < %@", startOfToday)
fetchRequest.predicate = predicate
Here's the code for the FreshReleaseTableViewController:
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
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
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 + "'snnew 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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
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 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
And here's the code for the ReleasedTableViewController:
import UIKit
import CoreData
import UserNotifications
class ReleasedTableViewController: 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
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: "album", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "artist", 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
tableView.reloadData()
override func numberOfSections(in tableView: UITableView) -> Int
return 1
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
return true
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: "Update", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
)
modifyAction.title = "Edit"
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
ios swift uitableview
add a comment |
up vote
0
down vote
favorite
I'm working on an app which is date based. I'm trying to get information to pass from one TableViewController to another.
It's a notification based app that reminds the user that something comes out today. So on the date it comes out, I'd like it to come off the release list and go onto the released list (So if something comes out the Nov.8th, on that date it'll be moved to the released list).
I'm having trouble getting it to work. I used the below codes and the date comes and goes without anything happening, it still shows it on the release list.
Below is the code I used on the release list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date > %@", startOfToday)
fetchRequest.predicate = predicate
And here's the code on the released list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date < %@", startOfToday)
fetchRequest.predicate = predicate
Here's the code for the FreshReleaseTableViewController:
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
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
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 + "'snnew 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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
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 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
And here's the code for the ReleasedTableViewController:
import UIKit
import CoreData
import UserNotifications
class ReleasedTableViewController: 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
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: "album", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "artist", 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
tableView.reloadData()
override func numberOfSections(in tableView: UITableView) -> Int
return 1
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
return true
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: "Update", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
)
modifyAction.title = "Edit"
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
ios swift uitableview
Don't use your view as if it was a model. Table views should not be passing data to other views. Create an underlying model and have it inform both table views as to what to display.
– Daniel T.
Nov 10 at 15:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm working on an app which is date based. I'm trying to get information to pass from one TableViewController to another.
It's a notification based app that reminds the user that something comes out today. So on the date it comes out, I'd like it to come off the release list and go onto the released list (So if something comes out the Nov.8th, on that date it'll be moved to the released list).
I'm having trouble getting it to work. I used the below codes and the date comes and goes without anything happening, it still shows it on the release list.
Below is the code I used on the release list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date > %@", startOfToday)
fetchRequest.predicate = predicate
And here's the code on the released list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date < %@", startOfToday)
fetchRequest.predicate = predicate
Here's the code for the FreshReleaseTableViewController:
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
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
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 + "'snnew 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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
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 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
And here's the code for the ReleasedTableViewController:
import UIKit
import CoreData
import UserNotifications
class ReleasedTableViewController: 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
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: "album", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "artist", 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
tableView.reloadData()
override func numberOfSections(in tableView: UITableView) -> Int
return 1
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
return true
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: "Update", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
)
modifyAction.title = "Edit"
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
ios swift uitableview
I'm working on an app which is date based. I'm trying to get information to pass from one TableViewController to another.
It's a notification based app that reminds the user that something comes out today. So on the date it comes out, I'd like it to come off the release list and go onto the released list (So if something comes out the Nov.8th, on that date it'll be moved to the released list).
I'm having trouble getting it to work. I used the below codes and the date comes and goes without anything happening, it still shows it on the release list.
Below is the code I used on the release list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date > %@", startOfToday)
fetchRequest.predicate = predicate
And here's the code on the released list:
let startOfToday = Calendar.current.startOfDay(for: Date()) as NSDate
let predicate = NSPredicate(format: "release_date < %@", startOfToday)
fetchRequest.predicate = predicate
Here's the code for the FreshReleaseTableViewController:
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
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
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 + "'snnew 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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
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 func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
And here's the code for the ReleasedTableViewController:
import UIKit
import CoreData
import UserNotifications
class ReleasedTableViewController: 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
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: "album", ascending: true)
let sortDescriptor2 = NSSortDescriptor(key: "artist", 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
tableView.reloadData()
override func numberOfSections(in tableView: UITableView) -> Int
return 1
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
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 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
return true
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: "Update", handler: (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
print("Update action ...")
success(true)
)
modifyAction.title = "Edit"
modifyAction.backgroundColor = .blue
return UISwipeActionsConfiguration(actions: [modifyAction])
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool
return true
ios swift uitableview
ios swift uitableview
edited Nov 10 at 16:00
rmaddy
236k27308374
236k27308374
asked Nov 10 at 15:18
sckring
115
115
Don't use your view as if it was a model. Table views should not be passing data to other views. Create an underlying model and have it inform both table views as to what to display.
– Daniel T.
Nov 10 at 15:24
add a comment |
Don't use your view as if it was a model. Table views should not be passing data to other views. Create an underlying model and have it inform both table views as to what to display.
– Daniel T.
Nov 10 at 15:24
Don't use your view as if it was a model. Table views should not be passing data to other views. Create an underlying model and have it inform both table views as to what to display.
– Daniel T.
Nov 10 at 15:24
Don't use your view as if it was a model. Table views should not be passing data to other views. Create an underlying model and have it inform both table views as to what to display.
– Daniel T.
Nov 10 at 15:24
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
There are a number of potential solutions to this.
The easiest I would propose is that your view controllers listen out for UIApplicationDidBecomeActiveNotification
. When this notification fires then you can run rebuild your fetch request and execute it again. This should then give you the up-to-date variation of the list.
An alternative option is to slightly change your data model to include an isReleased
flag in your data model. Now in your app delegate you can implement applicationDidBecomeActive
. When this method is called you can trigger an update of your database to refresh the isReleased
flag. In your view controllers you can make use of NSFetchedResultController
and change the predicate of your query to:
// Unreleased movies
fetchRequest.predicate = NSPredicate(format: "isReleased = NO")
// Released movies
fetchRequest.predicate = NSPredicate(format: "isReleased = YES")
The fetched result controllers have a delegate protocol that lets you listen for changes to your underlying data store, giving you an opportunity to update the UI.
I have this added: func applicationDidBecomeActive(_ application: UIApplication) in the AppDelegate.Swift file. Is this the appropriate location for it? If so, I still can't get the information over to the released list.
– sckring
Nov 11 at 2:13
I’m not to familiar with changing the data model. How would I go about setting the isReleased flag?
– sckring
Nov 11 at 13:23
I am assuming that you are using Core Data. You would need to add the property to your data model file on theRelease_Date
entity.
– marcus.ramsden
Nov 12 at 7:20
I added the isReleased flag to the Core Data and still can't get it to work. The only thing I can think of is that maybe I placed the NSFetchedResultController in the wrong place? Do I need to add this class as well? class NSFetchedResultsController<ResultType> : NSObject where ResultType : NSFetchRequestResult
– sckring
Nov 14 at 21:32
NSFetchedResultsController<NSFetchRequestResult>() fetchRequest.predicate = NSPredicate(format: "isReleased = YES") tableView.reloadData() }
– sckring
Nov 14 at 21:33
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
There are a number of potential solutions to this.
The easiest I would propose is that your view controllers listen out for UIApplicationDidBecomeActiveNotification
. When this notification fires then you can run rebuild your fetch request and execute it again. This should then give you the up-to-date variation of the list.
An alternative option is to slightly change your data model to include an isReleased
flag in your data model. Now in your app delegate you can implement applicationDidBecomeActive
. When this method is called you can trigger an update of your database to refresh the isReleased
flag. In your view controllers you can make use of NSFetchedResultController
and change the predicate of your query to:
// Unreleased movies
fetchRequest.predicate = NSPredicate(format: "isReleased = NO")
// Released movies
fetchRequest.predicate = NSPredicate(format: "isReleased = YES")
The fetched result controllers have a delegate protocol that lets you listen for changes to your underlying data store, giving you an opportunity to update the UI.
I have this added: func applicationDidBecomeActive(_ application: UIApplication) in the AppDelegate.Swift file. Is this the appropriate location for it? If so, I still can't get the information over to the released list.
– sckring
Nov 11 at 2:13
I’m not to familiar with changing the data model. How would I go about setting the isReleased flag?
– sckring
Nov 11 at 13:23
I am assuming that you are using Core Data. You would need to add the property to your data model file on theRelease_Date
entity.
– marcus.ramsden
Nov 12 at 7:20
I added the isReleased flag to the Core Data and still can't get it to work. The only thing I can think of is that maybe I placed the NSFetchedResultController in the wrong place? Do I need to add this class as well? class NSFetchedResultsController<ResultType> : NSObject where ResultType : NSFetchRequestResult
– sckring
Nov 14 at 21:32
NSFetchedResultsController<NSFetchRequestResult>() fetchRequest.predicate = NSPredicate(format: "isReleased = YES") tableView.reloadData() }
– sckring
Nov 14 at 21:33
add a comment |
up vote
0
down vote
There are a number of potential solutions to this.
The easiest I would propose is that your view controllers listen out for UIApplicationDidBecomeActiveNotification
. When this notification fires then you can run rebuild your fetch request and execute it again. This should then give you the up-to-date variation of the list.
An alternative option is to slightly change your data model to include an isReleased
flag in your data model. Now in your app delegate you can implement applicationDidBecomeActive
. When this method is called you can trigger an update of your database to refresh the isReleased
flag. In your view controllers you can make use of NSFetchedResultController
and change the predicate of your query to:
// Unreleased movies
fetchRequest.predicate = NSPredicate(format: "isReleased = NO")
// Released movies
fetchRequest.predicate = NSPredicate(format: "isReleased = YES")
The fetched result controllers have a delegate protocol that lets you listen for changes to your underlying data store, giving you an opportunity to update the UI.
I have this added: func applicationDidBecomeActive(_ application: UIApplication) in the AppDelegate.Swift file. Is this the appropriate location for it? If so, I still can't get the information over to the released list.
– sckring
Nov 11 at 2:13
I’m not to familiar with changing the data model. How would I go about setting the isReleased flag?
– sckring
Nov 11 at 13:23
I am assuming that you are using Core Data. You would need to add the property to your data model file on theRelease_Date
entity.
– marcus.ramsden
Nov 12 at 7:20
I added the isReleased flag to the Core Data and still can't get it to work. The only thing I can think of is that maybe I placed the NSFetchedResultController in the wrong place? Do I need to add this class as well? class NSFetchedResultsController<ResultType> : NSObject where ResultType : NSFetchRequestResult
– sckring
Nov 14 at 21:32
NSFetchedResultsController<NSFetchRequestResult>() fetchRequest.predicate = NSPredicate(format: "isReleased = YES") tableView.reloadData() }
– sckring
Nov 14 at 21:33
add a comment |
up vote
0
down vote
up vote
0
down vote
There are a number of potential solutions to this.
The easiest I would propose is that your view controllers listen out for UIApplicationDidBecomeActiveNotification
. When this notification fires then you can run rebuild your fetch request and execute it again. This should then give you the up-to-date variation of the list.
An alternative option is to slightly change your data model to include an isReleased
flag in your data model. Now in your app delegate you can implement applicationDidBecomeActive
. When this method is called you can trigger an update of your database to refresh the isReleased
flag. In your view controllers you can make use of NSFetchedResultController
and change the predicate of your query to:
// Unreleased movies
fetchRequest.predicate = NSPredicate(format: "isReleased = NO")
// Released movies
fetchRequest.predicate = NSPredicate(format: "isReleased = YES")
The fetched result controllers have a delegate protocol that lets you listen for changes to your underlying data store, giving you an opportunity to update the UI.
There are a number of potential solutions to this.
The easiest I would propose is that your view controllers listen out for UIApplicationDidBecomeActiveNotification
. When this notification fires then you can run rebuild your fetch request and execute it again. This should then give you the up-to-date variation of the list.
An alternative option is to slightly change your data model to include an isReleased
flag in your data model. Now in your app delegate you can implement applicationDidBecomeActive
. When this method is called you can trigger an update of your database to refresh the isReleased
flag. In your view controllers you can make use of NSFetchedResultController
and change the predicate of your query to:
// Unreleased movies
fetchRequest.predicate = NSPredicate(format: "isReleased = NO")
// Released movies
fetchRequest.predicate = NSPredicate(format: "isReleased = YES")
The fetched result controllers have a delegate protocol that lets you listen for changes to your underlying data store, giving you an opportunity to update the UI.
edited Nov 10 at 15:34
answered Nov 10 at 15:28
marcus.ramsden
2,1831828
2,1831828
I have this added: func applicationDidBecomeActive(_ application: UIApplication) in the AppDelegate.Swift file. Is this the appropriate location for it? If so, I still can't get the information over to the released list.
– sckring
Nov 11 at 2:13
I’m not to familiar with changing the data model. How would I go about setting the isReleased flag?
– sckring
Nov 11 at 13:23
I am assuming that you are using Core Data. You would need to add the property to your data model file on theRelease_Date
entity.
– marcus.ramsden
Nov 12 at 7:20
I added the isReleased flag to the Core Data and still can't get it to work. The only thing I can think of is that maybe I placed the NSFetchedResultController in the wrong place? Do I need to add this class as well? class NSFetchedResultsController<ResultType> : NSObject where ResultType : NSFetchRequestResult
– sckring
Nov 14 at 21:32
NSFetchedResultsController<NSFetchRequestResult>() fetchRequest.predicate = NSPredicate(format: "isReleased = YES") tableView.reloadData() }
– sckring
Nov 14 at 21:33
add a comment |
I have this added: func applicationDidBecomeActive(_ application: UIApplication) in the AppDelegate.Swift file. Is this the appropriate location for it? If so, I still can't get the information over to the released list.
– sckring
Nov 11 at 2:13
I’m not to familiar with changing the data model. How would I go about setting the isReleased flag?
– sckring
Nov 11 at 13:23
I am assuming that you are using Core Data. You would need to add the property to your data model file on theRelease_Date
entity.
– marcus.ramsden
Nov 12 at 7:20
I added the isReleased flag to the Core Data and still can't get it to work. The only thing I can think of is that maybe I placed the NSFetchedResultController in the wrong place? Do I need to add this class as well? class NSFetchedResultsController<ResultType> : NSObject where ResultType : NSFetchRequestResult
– sckring
Nov 14 at 21:32
NSFetchedResultsController<NSFetchRequestResult>() fetchRequest.predicate = NSPredicate(format: "isReleased = YES") tableView.reloadData() }
– sckring
Nov 14 at 21:33
I have this added: func applicationDidBecomeActive(_ application: UIApplication) in the AppDelegate.Swift file. Is this the appropriate location for it? If so, I still can't get the information over to the released list.
– sckring
Nov 11 at 2:13
I have this added: func applicationDidBecomeActive(_ application: UIApplication) in the AppDelegate.Swift file. Is this the appropriate location for it? If so, I still can't get the information over to the released list.
– sckring
Nov 11 at 2:13
I’m not to familiar with changing the data model. How would I go about setting the isReleased flag?
– sckring
Nov 11 at 13:23
I’m not to familiar with changing the data model. How would I go about setting the isReleased flag?
– sckring
Nov 11 at 13:23
I am assuming that you are using Core Data. You would need to add the property to your data model file on the
Release_Date
entity.– marcus.ramsden
Nov 12 at 7:20
I am assuming that you are using Core Data. You would need to add the property to your data model file on the
Release_Date
entity.– marcus.ramsden
Nov 12 at 7:20
I added the isReleased flag to the Core Data and still can't get it to work. The only thing I can think of is that maybe I placed the NSFetchedResultController in the wrong place? Do I need to add this class as well? class NSFetchedResultsController<ResultType> : NSObject where ResultType : NSFetchRequestResult
– sckring
Nov 14 at 21:32
I added the isReleased flag to the Core Data and still can't get it to work. The only thing I can think of is that maybe I placed the NSFetchedResultController in the wrong place? Do I need to add this class as well? class NSFetchedResultsController<ResultType> : NSObject where ResultType : NSFetchRequestResult
– sckring
Nov 14 at 21:32
NSFetchedResultsController<NSFetchRequestResult>() fetchRequest.predicate = NSPredicate(format: "isReleased = YES") tableView.reloadData() }
– sckring
Nov 14 at 21:33
NSFetchedResultsController<NSFetchRequestResult>() fetchRequest.predicate = NSPredicate(format: "isReleased = YES") tableView.reloadData() }
– sckring
Nov 14 at 21:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240343%2fgetting-data-to-transfer-from-one-tableviewcontroller-to-another-once-a-date-pas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Don't use your view as if it was a model. Table views should not be passing data to other views. Create an underlying model and have it inform both table views as to what to display.
– Daniel T.
Nov 10 at 15:24