In my last article, we learned the Create/Insert operation.So now, we will move to the second step of CRUD operations; i.e Read/Display the added records on our UI.
For my last article about inserting records into the database, please refer to the
link.
In my last article, we had created a UI which takes Name, Age and Phone Number as input and inserts the record into our database. We had performed this operation using Core Data Framework. So let's continue to the next operation.
- Open the project in which you have implemented the Create/Insert operation in Xcode. In my project, I want the records inserted in the database to be displayed on a table. So, I am going to add a tableview on my UI.
- Open Main.storyboard. Drag and drop a UITableView to your UI.
- On the top of the view controller, you can see 3 icons - View Controller, First Responder and Exit. Select the tableView, press Ctrl button and with the Ctrl button pressed, drag the mouse towards the yellow icon on the top of the ViewController. Once you select the yellow icon, a black box appears. Select 'dataSource' from the black box.
- Repeat the above step and this time, select 'delegate' from the black box. If you see the white dots as shown in the below image, that means you have selected them.
- Note: Connecting the tableview to datasource and delegate on UI is important. If you miss this step, your data will not be displayed.
- Now, connect your tableview outlet to your ViewController using Assistant.
- Now, we need to create a tableview cell for our tableview. Right-click on your Project Folder and select New File.
- Select iOS -> Cocoa Touch Class and click Next.
- Select UITableviewCell for SubClass of: and name your file. Also, select the checkbox 'Also create XIB File' and select the language as Swift. Click on Next and click on Create.
- This will add 2 new files to your project, an XIB file and a Swift File.
- Open the .xib file. Select the TableView Cell and assign an Identifier to this cell in its Attributes Inspector. I have assigned the Identifier as 'details'.
- Add 3 Labels inside the TableViewCell.
- Connect the labels to the .Swift file that you had created with this .xib file using the Assistant.
- Your .Swift file will contain the below code.
- import UIKit
- class DetailsTableViewCell: UITableViewCell {
- @IBOutlet weak
- var tblLblPhoneNo: UILabel!@IBOutlet weak
- var tblLblAge: UILabel!@IBOutlet weak
- var tblLblName: UILabel!override func awakeFromNib() {
- super.awakeFromNib()
-
- }
- override func setSelected(_ selected: Bool, animated: Bool) {
- super.setSelected(selected, animated: animated)
-
- }
- }
- Now we will add the code to display our data on our tableview.
- Open ViewController and add UITableViewDelegate and UITableViewDataSource to the line class ViewController: UIViewController.
- class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
- When you add them, you will get an error, just click on the error and fix them and Xcode will automatically add two functions of tableView.
- Add the below line in your ViewController.
- var details:[NSManagedObject] = []
- Now, add the below code in the viewDidLoad() function.
- tableView.register(UINib(nibName: "DetailsTableViewCell", bundle: nil), forCellReuseIdentifier: "details")
- We add the above code to register our xib. Every time we create an .xib File, we need to register them in our ViewController.
- Paste the below code after the viewDidLoad() function.
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
-
- guard
- let appDelegate = UIApplication.shared.delegate as ? AppDelegate
- else {
- return
- }
- let managedContext = appDelegate.persistentContainer.viewContext
-
- let fetchRequest = NSFetchRequest < NSManagedObject > (entityName: "Details")
-
- do {
- details =
- try managedContext.fetch(fetchRequest)
- } catch
- let error as NSError {
- print("Could not fetch. \(error), \(error.userInfo)")
- }
- }
- This code fetches the data from the database. NSFetchRequest executes the fetch request using current managed object context.This method must be called from within a block submitted to a managed object context.
- In the function of Add button, add the line details.append(record) in the do{} block after the line try managedContext.save(). This will add the NSManagedObject data to [NSManagedObject] array.
- do{
- try managedContext.save()
- details.append(record)
- Paste the below code in the tableView functions that the XCode had automatically added.
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) - > Int {
- return details.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell {
- let person = details[indexPath.row]
- let cell = tableView.dequeueReusableCell(withIdentifier: "details",
- for: indexPath) as!DetailsTableViewCell
- cell.tblLblName?.text = (person.value(forKey: "name") ? ? "-") as ? String
- cell.tblLblAge?.text = String(describing: person.value(forKey: "age") ? ? "-")
- cell.tblLblPhoneNo?.text = String(describing: person.value(forKey: "phone") ? ? "-")
- return cell
- }
- In the above code, I had added ?? "-" so that if the user does not enter input in any of the fields, then it will display "-" instead of blank field.
- Overall, the ViewController will contain the code as follows,
- import UIKit
- import CoreData
- class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
- var details: [NSManagedObject] = []
- @IBOutlet weak
- var tableView: UITableView!@IBOutlet weak
- var txtPhoneNo: UITextField!@IBOutlet weak
- var txtAge: UITextField!@IBOutlet weak
- var txtName: UITextField!@IBOutlet weak
- var lblPhoneNo: UILabel!@IBOutlet weak
- var lblAge: UILabel!@IBOutlet weak
- var lblName: UILabel!override func viewDidLoad() {
- super.viewDidLoad()
- tableView.register(UINib(nibName: "DetailsTableViewCell", bundle: nil), forCellReuseIdentifier: "details")
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
-
- guard
- let appDelegate = UIApplication.shared.delegate as ? AppDelegate
- else {
- return
- }
- let managedContext = appDelegate.persistentContainer.viewContext
-
- let fetchRequest = NSFetchRequest < NSManagedObject > (entityName: "Details")
-
- do {
- details =
- try managedContext.fetch(fetchRequest)
- } catch
- let error as NSError {
- print("Could not fetch. \(error), \(error.userInfo)")
- }
- }
- @IBAction func btnAdd(_ sender: Any) {
-
- guard
- let appDelegate = UIApplication.shared.delegate as ? AppDelegate
- else {
- return
- }
- let managedContext = appDelegate.persistentContainer.viewContext
-
- let entity = NSEntityDescription.entity(forEntityName: "Details", in : managedContext) !
-
- let record = NSManagedObject(entity: entity, insertInto: managedContext)
-
- record.setValue(txtName.text, forKey: "name")
- record.setValue(Int16(txtAge.text!), forKey: "age")
- record.setValue(Int16(txtPhoneNo.text!), forKey: "phone")
- do {
- try managedContext.save()
- details.append(record)
- print("Record Added!")
-
- let alertController = UIAlertController(title: "Message", message: "Record Added!", preferredStyle: .alert)
- let OKAction = UIAlertAction(title: "OK", style: .default) {
- (action: UIAlertAction!) in
- }
- alertController.addAction(OKAction)
- self.present(alertController, animated: true, completion: nil)
- } catch
- let error as NSError {
- print("Could not save. \(error),\(error.userInfo)")
- }
- self.tableView.reloadData()
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) - > Int {
- return details.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) - > UITableViewCell {
- let person = details[indexPath.row]
- let cell = tableView.dequeueReusableCell(withIdentifier: "details",
- for: indexPath) as!DetailsTableViewCell
- cell.tblLblName?.text = (person.value(forKey: "name") ? ? "-") as ? String
- cell.tblLblAge?.text = String(describing: person.value(forKey: "age") ? ? "-")
- cell.tblLblPhoneNo?.text = String(describing: person.value(forKey: "phone") ? ? "-")
- return cell
- }
- }
- We have added the code in our project to display our records in our tableview. Lets run the project and test if it works.
- If everything goes well then we must get the following output.
Note
If your application crashes or you do not get the expected output, re-check if you have properly followed the above steps. Here are some of the points that you may have missed,
- Check if you have followed these steps in your Main.storyboard,
- On the top of the view controller, you can see 3 icons - View Controller, First Responder and Exit. Select the tableView, press Ctrl button and with the Ctrl button pressed, drag the mouse towards the yellow icon on the top of the ViewController. Once you select the yellow icon, a black box appears. Select dataSource option from the black box.
- Repeat the above step and this time, select delegate option from the black box. If you see the white dots as shown in the below image, that means you have selected them.
- Check if you have assigned Identifier to your TableViewCell in the xib file and you have used the same identifier in your code.
- Check if you have registered your xib file in your ViewController.
- I have noticed that if I give the input of the phone number more than 5-digits then it does not display on my output. This is because we have assigned the data type of phone as Integer16 in our .xcdatamodeld file. You will have to change the data type to Integer32/Integer64/String. If you do so, make sure you delete the existing application from your simulator/iPhone device before running it again otherwise the application will crash. Also make sure, you have made the changes in the code wherever Integer16 was used.
So, we are now successfully able to add our records in our database and display them on the tableview. In my next article, we will perform the remaining 2 CRUD operations - Update and Delete.