Cleaner way to handle UITableViewCell

Its really funny and confusing when you start to implement the cellForRow or itemForRow datasource method.  Usually we do that in the ViewController, or in a special dataSource object, and you find yourself asking where should you add that piece of code responsible for configuring or handling the Cell, you would then add it in the Cell subclass itself or just bring more code to the ViewController that is barely controlling his direct view(s).

In a non-trivial world you won’t just set the title and description for the Cell, ending up with 2 to 3 lines, no!, there will be buttons, images , special layout , events and a whole set of operations going on, that is when you handle that in the ViewController it’d be messy and looks so wrong!!!

MVC

Yup , Just like UIViewController , we need a UITableViewCellController, its a bit long name so we will use CellController , since it can be a table view cell or collection view cell, or any list based views element.

WHY

simply to isolate the mess of handling a cell.

How does it look like?

class UserCellController {

let cell: UserTableViewCell
let model: UserModel

init(cell: UserTableViewCell, model: UserModel) {
self.model  = model
self.cell  = cell
}

func configure() {
fill()
style()
}

// here we fill view items

private func fill() {
//e.g. namelabel.text = model.name
}

private func style() {
//e.g. namelabel.cornerRadius = 10
}

}

Inside cellForITem or the method you handle the filling the cell. 

let model = ….

let cell = …

let cellcontroller = UserCellController(cell: cell , model: model)

cellcontroller.configure()

Now you can fill,configure and style the cell based on the model, you can validate the model and modify the input or anything else inside the CellController instead of the tableView datasource method.

As you noted there is a loose end here, as the CellController object should not live in memory for long, and its pointing strongly to the cell object, given the same cell object should be reused and filled with other content.

Based on how many cells can be visible, the table will allocate certain number of cells and keep reusing them.

To solve that we will keep these controllers a live by adding them to dictionary inside the Viewcontroller in our case.

private var controllers: [Int: UserCellController] = [:]

and inside cellForRow…

let cellcontroller = UserCellController(cell: cell , model: model)

cell.configure()

self.controllers[cell.hash] = controller

The trick here is to use cell.hash as there are a fixed number of hashes since cells are reusable inside a tableView .

Actions

now we can handle filling and styling the cell easily , what about actions coming from the Cell itself, what if you have a button inside the cell and you want to handle it inside the ViewController since its probably something bigger than our little CellController can handle.

In that case , it is as simple as adding completion handler to the CellController constructor, or add call back as property , or  Streams as in RxSwift as property , or delegate Oo (just kidding,don’t) …

 

FINALE

That should be it , you got yourself a separate controller that can handle list items more cleanly than dumping all the functionality inside the tableView datasource since life is not that simple and you will need to have a lot of functionalities handled by the Cell.

 

Useful Tools and services for iOS development 


I’d like to share a few tools I use when I work on iOS projects.

Fabric

Consist of a set of great tools that you can easily integrate to your app , one of the most valuables tools for me is Crashlytics where you can send Beta builds and receive crashes when they occur. You can add as many apps freely and manage them, and you can have teams with ability to manage their access. Also service hooks is really handy e.g. send a slack message when a crash reaches some level on some App. Answers tool will give you Analytics with many options as setting events ,observing retention, engagement and many more.. All you need is download the fabric app and it will guide you through the integration seamlessly. There is an iOS app for fabric for tracking your apps.

Fastlane

Automate your deployment from generating certificates to uploading to the App Store , it’s wonderful and time saving. One of my favorite tools is match which will automate certification and provisioning for the whole team using git, along with register_devices action you will be able to add new devices , recreate certs and profile without even visiting developer.apple.com (you still need to have access to the apple account). It has many actions and the docs is neat, you may check the many examples as well. (Having some knowledge of ruby will be very handy to you).

Cocoapods

The famous dependency manager for swift and Obj-c. It allows you to use open source / private libraries easily, few commands and your up to go. It makes it easy to update your dependencies and easy for Pods makers to update their Pods, so when there is new swift version , all you have to do is to update the Pods and you should be fine (JK you still have to suffer 😂😅, but it will be easier 😄). Another important feature is private pods that you may share with your team privately.

Another famous dependency manager is Carthage which I don’t use a lot, but one thing good about it is that it makes build time faster, so if you are using a big and well known frameworks you may consider using carthage for them, and leaving smaller and less complete libraries in Cocoapods so that you can change and experiment on them on Spot.

R.swift

An awesome tool for code generation from static files such as fonts , images , string files , nibs/storyboards, colors from .clr files.. which means no miss spelling errors and you get compile time assistant. I made this script that will generate Obj-c class from localizable.string which is a good practice to have your Strings encapsulated in methods or properties ..

JSONExport

It allows you to generate Model classes from JSON Objects, which is very useful for mapping API responses. It supports Java,Obj-C, and Swift, with number of libraries like Mappable, Realm, CoreData … JSON Accelerator is another amazing tool with the same goal for obj-c ,java, and Python.

Makeappicon

To generate app icons including icons for App Store, all you need is to upload 1024 icon and you will get all icon sizes for ios,android, and watch os.

Appetize

Upload an IPA and send it to the customer and they can view it on the web, no UDID needed!!!. Its great for demoing an example of a library or framework you made instead of asking the user to download and integrate or making a gif!!!. Really useful and amazing.

Sourcetree

This is the best GUI tool you can get to deal with Git and its free!!. It make working with Git a breeze. You get many options  like commit,push,merge,rebase,stash,resolve conflicts,reset , log all commits for certain file and revert to any version , add to index ,remove ,ignore ,checkout from any commit and more without the need to use terminal… though you still have terminal button to use terminal for more advanced operations .
If you use bitbucket like me, you get clone in sourcetree button for faster cloning as bonus, but you can have account added in the App and you can fetch all your repositories and clone any without going to bitbucket website.

Another tool is Github Desktop but with way less feature than sourcetree. Still useful to have it to use the open in desktop button in github !!.

CocoaControls

Its awesome website that you should check if you have not, where you can find amazing controls for iOS, macOS, watchOS,and tvOS. there are thousands of useful open source projects mainly as UI Components. Most of the New Projects have appetize embedded and you can try the control before using it. I would suggest to keep checking it from time to time , and to try to contribute in the projects.

xliff.brightec

awesome website for importing and generating xliff files for iOS, you simply import your xliff file, update the translation on the website and then export it to be used in Xcode.  Localise.biz  is not free but you can generate android xml and iOS .string localization files altogether.


Commercial

Paw (trial)

Best API Tool I ever used , what makes it great is that it can generate code for you in many languages, which you can use for fast prototyping. Paw has ability to add extensions for Importers (e.g. API blueprint, swagger, postman) and Exporters (curl, swift , java ..). and many other features.

Sketch (trial)

The best tool to design a mobile app, its simple to use for developers, where you can easily explore the design given by the designer and export its elements, figure out the sizes and colors , and modify the design easily. It’s even simpler for non-designers like me to use and make nice designs with help of free resources like this amazing website https://www.sketchappsources.com .

One amazing thing is that its pluggable where you can find many plug ins that do awesome features like AutoLayout by anime . Another nice thing is that it’s vector based where you can design in 1x and export in 3x and more , plus with the ability of Xcode to handle pdf you can export sketch resources in pdf :).

PaintCode (trial)

Will convert your SVG drawings and designs into Code ,swift/objc/android/javascript . Having the design in code will give you more abilities , simply you can customize anything like fill color, border colors/width/shape, etc.. and one important thing is Animation, PaintCode have a feature called Variable which makes it easy play with things like colors, positions, rotations …

#@end

These are useful subset of tools That I use always and works perfectly for me. There are many great tools out there that can make your life better and it’s good to try and see, one website that can help in that is thoughtworks.com.

Xcode : Create color Palette in Interface builder

Every project has its own set of colors, and if you use Interface builder like me, then you probably would like to create a color set for each project so you don’t get mad when you want to set a color for some text and not finding it in the “Recently used Colors” Section !.

You can simply create your own color palette in IB by clicking on the color picker -> Color palettes -> Click on the settings icon and click on new. 🙂

After that you can click on the settings icon again and rename the palette. And then you can add the colors related to the project you want.

And here you can see how it’s done in pictures . Have fun.

 

screen-shot-2016-12-15-at-10-31-49-am screen-shot-2016-12-15-at-10-32-08-am screen-shot-2016-12-15-at-10-32-15-am screen-shot-2016-12-15-at-10-32-31-am screen-shot-2016-12-15-at-10-32-42-am screen-shot-2016-12-15-at-10-33-34-am

 

Xcode 8.1 umbrella header not found

I have an xcode project that I use as a framework.
I got this issue after upgrading to xcode 8.1 saying ” Umbrella header ‘nameofmodule.h’ not found’

screen-shot-2016-11-21-at-2-43-47-pm

 

 

 

 

Solution

create a header file and name it as your modulename is.

screen-shot-2016-11-21-at-2-45-38-pm screen-shot-2016-11-21-at-2-45-47-pm

 

 

 

 

 

 

 

in the header file add these lines

#ifdef __OBJC__

#import <UIKit/UIKit.h>

#endif

FOUNDATION_EXPORT double <#modulename#>swiftVersionNumber;

FOUNDATION_EXPORT const unsigned char <#modulename#>swiftVersionString[];

 

thats it.

 

 

Custom back button in UINavigationController

Snippet From Apple sample Code

UIImage *backButtonBackgroundImage = [UIImage imageNamed:@”back”];
    // The background should be pinned to the left and not stretch.
  float margin = 12.0;
    backButtonBackgroundImage = [backButtonBackgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, backButtonBackgroundImage.size.width – margin, 0, 0)];
    id appearance = [UIBarButtonItem appearanceWhenContainedIn:[
CustomBackButtonNavController class], nil];
    [appearance setBackButtonBackgroundImage:backButtonBackgroundImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:@” ” style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backBarButton;

Just one note : the image you use may have different size an though needs different margin Value ;

Just another: CustomBackButtonNavController   can be replaced with UINavigationController in case you are using the same back button all over the app as in my most cases 🙂 .