Real World Example: Creating a Journal Entry App with Core Data in Swift Part 2.

Farhan Syed
iOS App Development
4 min readApr 23, 2017

Just in case you missed part 1.

In part 2 I will be going over various issues that you’d encounter if you completed part 1.

Here’s somethings we will go over:

  • Deletion of an entry with slide to delete
  • Dismiss keyboard when pressing enter
  • Self-sizing cells to accommodate more text
  • Most recent entries should be at the top
  • Add date/time stamp to each entry
  • How to use custom RGB colors for UIKit elements

Deletion of an entry

So you don’t want to have that entry anymore?

Let’s go ahead and delete it.

Apple provides us with a nice easy to use set of methods for UITableView.

Let’s implement editActionsForRowAt

If you take a look at the function, we need to return an array of UITableViewRowAction(s) .

Let’s implement the deletion of the entry.

Maybe if this article get’s a lot of support, I’ll go over how to share our journal entries to Twitter.

Dismissal of keyboard

If you noticed in part 1, the keyboard by default doesn’t dismiss itself after the user presses Return.

Let’s fix that.

You need to add UITextViewDelegate to your addItemViewController or maybe you called it something else.

In the viewDidLoad we now need to set it’s delegate to self.

To get the keyboard to dismiss, we can add shouldChangeTextIn function.

Let’s add the ability to dismiss.

Command + R, see if it works!

Self-sizing cells

If you finished part 1, you may have noticed if we entered more text than a few words. The text would get cut off at the end.

Ideally we want it to look like this:

This one is easy and straightforward.

Note: This method won’t work with UIImageViews inside cells. Those require more complex calculations.

First if you used storyboard, open it and click on the title label. Set it’s number of lines to 0.

Then in your viewDidLoad let’s give the UITableView a estimatedRowHeight and set it’s rowHeight to UITableViewAutomaticDimension.

The first line is sets the estimatedRowHeight. In this case the height of the prototype cell.

The second line changes the rowHeight to UITableViewAutomaticDimension .

Command + R to see your cells grow!

Most recent on top

We usually see most recent items on top of our tables when we use apps.

In our case we saved our journal entries and every time we save it the newest one gets tacked on the back of the array when we retrieve it.

                      [<- Oldest, Newest ->]

A little hack we can do is simple use the reverse() function

We can apply this where we set the cell’s text.

Now your most recent entries should be on top.

Date/time stamp

Let’s add the date and time of each entry that was saved.

First let’s go back to the .xcdatamodelId .

Add two more attributes, date and time with type String.

Now open the .swift file you save your entries in.

Add this to your if else we created in part 1.

This is pretty straightforward.

You can mess around with different types of formatting with the dateFormat and timeStyle.

Now let’s save it.

To display this let’s go back to our UITableViewController.

Inside cellForRow update your code to this:

In our storyboard change your cell style to Subtitle.

Make sure you go back and change the text lines to 0 again since it may of gotten reset. If you forget you won’t have your cells resize.

Now we have our date & time on each entry.

For fun

Wondering how I got the flat green color?

I use this website to look at flat colors for some colors in my projects for fun.

You can apply them via code or in storyboard.

I’ll show you both.

Code

For RGB values, you need to always divide by 255.

Put this code right before the return in editActionsForRowAt .

Storyboard

To do this in storyboard, simple click on the button or label you would like to change and click on it’s tint in the Attributes Inspector.

Now a pop up should show up something like this.

Click the second tab at the top to enter the RGB values.

That’s it!

I hope this helped you out.

Let me know any other topics you’d like for me to write about!

As always, any questions, comments or even criticism feel free to let me know!

Also, if this post gets enough support from the recommendations, I’ll show you how to share your journal entry to Twitter!

Thank you!

--

--