Weekly Swift 2, getting somewhere

Published on: February 6, 2015

Time flies, it’s been two weeks since I started my daily Swift adventure and I only missed one day. Pretty impressive I’d say. The second week of the daily Swift was a very practical one. It was all about looking through Arto App and building the components I’d need to actually build this app. Building it will be a part of the daily Swift.

Layout and Constraints

A big part of creating a beautiful feed of content seems to be understanding the UITableView. More specifically, understanding UITableViewCells. These Cells are the core of what a user will see and interact with and giants like Twitter, Facebook and Instagram all implement some custom version of a UITableViewCell.

Making my own cells appeared very hard at first. I didn’t understand what would go where, and how would I make it auto-resize itself to fit the content I put into in. After digging around on the web I found a video tutorial and a blog post that explained in part how to do this. Then I started experimenting with LayoutConstraints. These Constraints are extremely powerful and surprisingly simple to write. I’ve been getting warnings every time I used them but everything looks and works fine so I’m not sure how serious these warnings are. An example:

"V:|-10-[myObject]-10-|"

The text above defines a Constraint in Visual Format Language (VFL) and it says that on the vertical axis(V) there should be a 10 point margin (-10-), then myObject appears and then there’s another 10 point margin (-10-). The width of myObject in this case will be the container’s width minus two times a 10 point margin.

Delegation

In order to load data from the Arto API I wanted to have an object in my application that would manage this. I decided to use delegation as a callback style for this object. The implementation of this is actually pretty simple. In the same file as the one where I defined my API class I wrote a protocol. A protocol is defined by naming it and specifying methods that should be implemented by the implementer of this protocol.

Then, whenever the object finished loading some data I call:

self.delegate?.didReceiveData(data)

The fact that there’s a question mark after self.delegate  tells the compiler that there might not be a delegate. If the delegate isn’t there, everything after the question mark is ignored.

So all I needed to do to use this, is make an ArtoAPI instance in my TableViewController, implement the ArtoAPIDelegate protocol and use the data that gets passed to didReceiveData . Easy enough, right?

Bonus: A header image effect

There’s some apps that have this very nice expanding header effect when you pull down the content below it. I have recreated this effect and it actually was surprisingly easy.

Expanding header example

Putting it together

At the end of the week I’ve spent some time putting together the parts. Combining the UITableView, UITableViewCell, ArtoAPI, loading images with Haneke and more. A picture says more than a thousand words so here’s a gif that shows the end result.

Arto Feed Example

In conclusion

The second week of the daily Swift was all about working towards creating and iOS version of Arto App. I’m still trying to decide whether week three should keep pushing forward or if week three should be about learning more small things like an off-screen menu, blurring images, using shadows, implementing POST requests to push data to a server and more of that. If you can’t wait to find out, make sure to follow me on Twitter or check out my Github regularly. Thanks for taking the time to read this and if you have any tips, comments or feedback be sure to let me know.

This week's repositories

Categories

Uncategorized

Subscribe to my newsletter