Pre-Scrolling a Table to a specific cell at load

Although that’s something that appears fairly easy, the truth -for me at least-, is that scrolling a UITableView to a specific cell every time you load it may prove tricky. 

In the app I’m working on I implement it in the following way: every time a view appeared I assess if the top visible row is the one it should. If it is, no action is taken, else we’ll scroll to the right row. 

    override func viewDidAppear(_ animated: Bool) {


        //Check if we need to scroll to a specific cell

        if getTopVisibleRow() != getTableLastViewedTopPosition() {

            tableView.scrollToRow(at: IndexPath(row: getTableLastViewedTopPosition(), section: 0), at: .top, animated: false)




For reference here’s what I’m doing in the getTopVisibleRow(), note that is is important only if you’re using a translucid navbar:

    func getTopVisibleRow () -> Int {

        //We need this to accounts for the translucency below the nav bar

        let navBar = navigationController?.navigationBar

        let whereIsNavBarInTableView = tableView.convert(navBar!.bounds, from: navBar)

        let pointWhereNavBarEnds = CGPoint(x: 0, y: whereIsNavBarInTableView.origin.y + whereIsNavBarInTableView.size.height + 1)

        let accurateIndexPath = tableView.indexPathForRow(at: pointWhereNavBarEnds)

        return accurateIndexPath?.row ?? 0



For getTableLastViewedTopPosition() I’m simply fetching from the model what cell index should we be showing. 

Questions / comments? I’m at @MarcMasVi 


Find the right value in an Array, or in an Array of Dictionaries

From time to time I discover a new way of coding something that significantly improves readability & minimizes how much I need to code. 

Here’s one of such latest discoveries. Typically to find a value in an array (or in an array of dictionaries) with a specific condition I would do a for loop with an if statement checking for the right value. This works great of course, but its a lot of code to do something that simple. 

Here’s a simple way to find a value in an array of dictionaries:

let matchingArticle = arrayOfItems.first(where: {$0.exampleKey == 3843})


We ask the arrayOfItems to give us the first value where the dictionary key exampleKey takes the value 3843. 

And if it’s only an array of int or string, for example, is even easier:

 let matchingArticle = arrayOfItems.first(where: {$0 == 859})

We simply ask the arrayOfItems to give us the first value where the its value equals 859. 

Note, in this code example we assume you will always have only up to 1 match. 

I’ve come to love this function and I think you will too. 


Questions / comments / suggestions? I’m at @MarcMasVi