Swift Style: Dequeuing and Populating Cells From UITableView

Everyone has an opinion when it comes to Swift code style, and here is mine when it comes to dequeuing and populating cells from UITableView.

First you should know Apple maintains two methods for dequeuing cells from UITableView:

func dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell?

and

func dequeueReusableCell(withIdentifier identifier: String, for indexPath: IndexPath) -> UITableViewCell

The second version, with the indexPath argument addition and non-optional return value, was added in iOS 6 but strangely the original version was never marked as deprecated. The indexPath version is the one you should use. (I know of no reason why anyone should prefer the original, but I welcome feedback.)

Now let’s take a look at an implimentation:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: CustomCell.identifier, for: indexPath)
    if let customCell = cell as? CustomCell {
        customCell.item = itemForIndexPath(indexPath)
    }
    return cell
}

First we dequeue our cell. Notice how we use a class property to provide the cell identifier string value. We do it this way to avoid typos and enable easier refactoring.

Also note how we avoid any type casting on this dequeue line and instead capture the cell in a simple UITableViewCell reference. We do this because there is a chance that our tableview might not be properly registered with the cell identifier. Under such a scenario we’ll still get a cell instance but it will not be a CustomCell instance. I’d much rather return a boring UITableViewCell than crash with some explicit type casting using as! here.

Next we do our type casting using the more forgiving as?, making a new casted reference if a match happens.

To populate our cell there are two basic paterns. One would have a very generic cell expose its interface outlets and so you could configure the cell as you see fit depending on your model. The second approach would be to keep the cell’s outlets private and instead have the cell accept a model the cell is suppose to represent.

I generally lean towards the model approach. I also like to make a method called itemForIndexPath which helps if I ever refactor the view and introduce sections. If you are worried about coupling consider building a model just for the cell’s needs; a CellViewModel or something similar.

And finally, we return our cell reference. 👍

Hello Firefox, Goodbye Chrome

A few weeks ago I said to myself, enough was enough and dropped the Chrome web browser.

If you aren’t actively following the web development world it’s easy to miss, but Google and Chrome have really been hostile toward the open web over the last few years. A few examples:

In 2016 Google introduced Accelerated Mobile Pages. From its wikipedia page:

AMP has been widely criticized by many in the tech industry for being an attempt by Google to exert its dominance on the Web by dictating how websites are built and monetized, and that “AMP is Google’s attempt to lock publishers into its ecosystem”. AMP has also been linked to Google’s attempt to deprecate URLs so that users will not be able to immediately see whether they are viewing a webpage on the open Web or an AMP page that is hosted on Google’s servers. AMP has been described as being “poisonous to the underlying concept of an open internet.”

In Chrome 68 they began to deprecate HTTP and now list HTTP sites as insecure.

In Chrome 69 they added a behavior that strips the www subdomain from the URL bar. (Try visiting www.mikezornek.com and it becomes mikezornek.com. This may feel like a small thing for users but as a developer its a firm violation for how the web URL works.

Just this weekend I read a story of some more shenanigans:

Matthew Green:

A few weeks ago Google shipped an update to Chrome that fundamentally changes the sign-in experience. From now on, every time you log into a Google property (for example, Gmail), Chrome will automatically sign the browser into your Google account for you. It’ll do this without asking, or even explicitly notifying you.

Paul Frazee:

There’s a reason people are reacting to Chrome like this. This isn’t an overreaction over one single event. It’s a delayed reaction to a pattern of bad behavior.

It’s contextualized by the very messed-up power dynamic between Google and the open Web.

Google’s influence over the web through its search was already over the line in many ways but to see its once celebrated browser go down this road is depressing.

Actions speak and Firefox is treating me well. I now use it across my computers and devices, from MacOS to iOS and even my Windows gaming tower. DuckDuckGo is my default search engine (and has been for like a year now) and works well too. It feels good to be using products from people who share my interests to heart.

Apple’s Use of Donations as Marketing

The other day Tim Cook (‪@tim_cook‬) tweeted:

Our hearts go out to the people of Sulawesi and all of Indonesia after this weekend’s devastating earthquake and tsunami. Apple is donating $1 million to aid relief efforts as this beautiful country starts to rebuild.

I feel bad for the earthquake victims too, but this kind of marketing makes me a little ill.

Best I can tell Apple reported 53.8 billion dollars in profit over the last year. If you compared the ratio of their donation to say a person making $100,000 a year, the donation would be $1.86.

Now I don’t want to say a million dollars isn’t a big impact to the receivers of the donation, I’m sure it is — but it just feels a little cheap to announce it and have everyone retweet it, the media website repost it, all to build up this fake “good will” sentiment. Hell, you can’t even buy a media ad campaign for that cheap.

Anytime you read this stuff remember, these are the same big corporations dodging taxes that could normally fund proper emergency relief and preparedness along with other infrastructure needs.

Some might say, well Apple needs to answer to their shareholders and this is how every company works. Well I say, everyone has a choice, and yeah it can be easier to fall in line and do the expected but I root for the person who is going to step out of line and Think Different.

At a minimum, donate your $1.86 annomously without looking for a thank you.