Skip to content

Instantly share code, notes, and snippets.

@Darmaal
Created August 30, 2016 07:59
Show Gist options
  • Save Darmaal/84532d584fb434ef0d4e063fa3b74f31 to your computer and use it in GitHub Desktop.
Save Darmaal/84532d584fb434ef0d4e063fa3b74f31 to your computer and use it in GitHub Desktop.
import UIKit
import CoreData
class tableViewController: UITableViewController {
var listOfItems = [NSManagedObject]()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: Selector("addItem"))
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listOfItems.count
}
func addItem(){
let alertController = UIAlertController(title: "Type something", message: "please type....", preferredStyle: .Alert)
let confirmAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.Default, handler: ({
(_) in
if let field = alertController.textFields![0] as? UITextField {
self.saveItem(field.text!)
//reload our list to update it
self.tableView.reloadData()
}
}
))
//cancel action
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
//adding a text field
alertController.addTextFieldWithConfigurationHandler({
(textField) in
textField.placeholder = "Type in something!"
})
alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.presentViewController(alertController, animated: true, completion: nil)
}
func saveItem(itemToSave : String){
// this AppDelegete will control how our items are going to save
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entityz = NSEntityDescription.entityForName("ListEntity", inManagedObjectContext: managedContext)
let item = NSManagedObject(entity: entityz!, insertIntoManagedObjectContext: managedContext)
item.setValue(itemToSave, forKey: "item")
//here we try to save an item
do{
try managedContext.save()
listOfItems.append(item)
}
// this is where the try catch any potentiaonl error
catch{
print(" This is the the try method reporting an error Sir!")
}
}
// fetch every item in our core date and apply it to our list
override func viewWillAppear(animated: Bool) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//creating our fetch request
let fecthRequest = NSFetchRequest(entityName: "ListEntity")
do{
let results = try managedContext.executeFetchRequest(fecthRequest)
listOfItems = results as! [NSManagedObject]
}
catch {
print ("something went wrong")
}
}
//this func is built to remove items from our list
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
//first we remove the item from our core date
managedContext.deleteObject(listOfItems[indexPath.row])
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Right)
//second we remove the item from our list of items
listOfItems.removeAtIndex(indexPath.row)
//updating our list
self.tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let kalia = tableView.dequeueReusableCellWithIdentifier("Kalia")! as UITableViewCell
let item = listOfItems[indexPath.row]
kalia.textLabel?.text = item.valueForKey("item") as! String
return kalia
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment