Simpler and safer iOS custom table view cells with Reusable

When you create a UITableViewCell that you want to use with multiple UITableViews and design its view using a XIB file you know that registering and using it involves the usage of string constants. When you register your custom UITableViewCell with the UITableView you use a string as XIB name and a string as the reuse identifier. Then you use the string reuse identifier again to actually use it. There must be a better, safer way, right? Of course there is, just use Reusable.

Reusable is a Swift mixin for reusing views easily and in a type-safe way for UITableViewCells, UICollectionViewCells, custom UIViews, ViewControllers, Storyboards. It contains protocols you add to your classes and let the magic (the default implementation for those protocols) happen.

So how do you get rid of all those strings when using custom cells with UITableView? First, add the NibReusable protocol to you custom cell class

[Read More]

Making UITableView's header 'stickier'

Working on na iOS app I had to solve a interesting UI problem. The screen had to contain a UITableView with a header. The header should not have been visible when the screen was displayed. In fact the header should not have been visible when the user just scrolled the UITableView up and down, it only had to become visible when the user “dragged” the UITableView down, similar to doing pull to refresh. Scrolling the UITableView then hides the header again.

To better imagine the requirements, take a look at this animation

Notice that you see that the header is there but I have to really drag the UITableView to make it visible. It then disappears when I scroll the UITableView.

[Read More]

Formatting Swift code in XCode

When I started using XCode I was really surprised about the really poor implementation of its code formatting functionality. It kind of formats the alignment of the code but ignores unnecessary spaces and a lot of other things. Formatting the source code and keeping the style consistent is really important to me so I was looking for a solution. I found some linters like SwiftLint but I was interested in a tool that will actually format the source code for me on demand. I found SwiftFormat.

SwiftFormat

SwiftFormat is a code library and command-line tool for reformatting swift code. It applies a set of rules to the formatting and space around the code, leaving the meaning intact.

[Read More]

Using MVVM with tables and cells in iOS

When I ventured into native iOS development I immediately took a look at the possibility to use data binding on iOS which enables me to simply declare the relationships between the UI and the ViewModel. This article takes that approach further shows you how to use MVVM and data binding when using tables and cells, or in the world of iOS UITableView and UITableViewCell.

Sample scenario

Let’s start with simple example scenario. You want to show progress of some flow that contains of multiple steps, each of the steps can be either running or complete. When a step is running it can report its progress. You want to display this flow in a table that looks like this

[Read More]

Using data binding in iOS

When I started working on a native iOS project after a few years of Windows (Phone) development I looked into ways to write a more declarative and more elegant code than the “standard” iOS way. I wanted to transfer some of my habits over and the first thing I really missed was XAML data binding. I did some research on how to do data binding in iOS and found a few libraries that make it possible. This allowed me to write better code and I think data binding is a concept that all the iOS developer should look into. If you are interested in my experience with using binding in iOS, read on.

Sample scenario

Let’s use a simple example scenario. You have a screen where the users have to choose their country and then enter their mobile number. The number has to be validated with respect to the selected country and if everything is ok the Next button should become visible. So basically it should work like this

[Read More]