Settings

Theme

Building an Open, Drop-in Replacement for UITableView

techblog.livingsocial.com

39 points by wastedbrains 12 years ago · 17 comments

Reader

bengotow 12 years ago

Overall this is a great article. It does a good job outlining what UITableView does and why it's built the way it is. If you're new to iOS and wondering why UITableView's delegate and dataSource protocols are structured the way they are, it's a good idea to take a deep dive and understand them better. As others have mentioned, data sources and delegates aren't unusual in Objective-C. They're everywhere, and they enforce a separation of view logic and data that is at the heart of MVC.

This is a great exercise, but please, please don't use your hand-rolled UITableView in your apps.

If UITableView doesn't do what you need, create a UICollectionView. If you find the data source protocol frustrating to use, create a provider object that maps your (array/dictionary/weird-ass data structure) onto the dataSource protocol. (In desktop Mac programming, NSArrayControllers serve this purpose.) Don't re-implement UITableView. The things you could potentially screw up are many, and when some other developer inherits your code and tries to add editing, multiple selection, re-ordering, etc..., they're going to be pissed.

prewett 12 years ago

Is it just me, or does it seem like UITableView is a combination of controller and view? The delegate has both controller functionality (selection) and view functionality (cell height). I keep trying to use it as one or the other and you just can't do it.

Although, my view of MVC is that the model is just the data (pretty lightweight), the view just draws the things, and the controller handles user actions. So you could remove the view and do testing on the controller. This doesn't seem to be Apple's perspective, so maybe I just haven't figured out their perspective yet.

  • joshavant 12 years ago

    In iOS, sometimes the line between view and controller gets blurred.

    UIViewController is part of UIKit. UIKit is a framework developed specifically for iOS as a high-level wrapper over OpenGL, for performance reasons as well as making touch screen based interfaces easier to implement.

    This is why UIViewController has many view-related hooks in it. For example, the native view lifecycle flow includes a call to the method `viewDidLoad` on UIViewController - a controller method dedicated to view functionality and customization.

    That said, the UITableViewDelegate seems to have the standard amount of view and controller commingling that's found throughout the common iOS frameworks, IMO.

brendanlim 12 years ago

Nice job creating your own implementation of UITableView. Although, if you find delegates and datasources unusual then its best for you to spend more time getting familiar with them rather than trying to work around them.

  • joshavant 12 years ago

    Hi! Post author here.

    Totally agree with that last statement.

    That said, I tried to write the post to be geared towards the beginner audience. And I remember when I first started iOS development, UITableView seemed weird.

    It comes with two delegate objects with separate responsibilities (delegate and dataSource) and is pretty ubiquitous, so you may interact with it right away when first starting iOS development, before interacting with the many other classes that use delegates (and the, what, 3 others that use dataSources).

    To a beginner, that tends to make UITableView stand out as an unusual thing.

    I'm hoping pulling UITableView apart, and explaining the machinery can help make those design decisions more clear.

    • vonseel 12 years ago

      I am an iOS beginner.

      I have not read your post in-depth, I will, but I think it's probably wise to stick to learning UITableView well -- and understanding why I would want to implement something custom -- before avoiding the classes Apple provides.

      That being said, I understood delegates, categories, and other Objective-C features much better after coding several applications first, reading a ton of open source code, and then reading relevant parts of Programming in Objective-C (Kochan). I first purchased his book before attempting to code for iOS, and found the pace too slow or concepts too unfamiliar, but after some practice and finding what I did not understand, it is much easier to look to documentation and textbooks for appropriate reference.

      • girvo 12 years ago

        The OP didn't reimplement it to use it, it was done to explain. that's what I got out of it anyway.

    • ocrickard 12 years ago

      Here's to demystifying the internals of UITableView! Your tone seemed fine to me. Glad you geared it towards newer folks to iOS. I remember when I was first starting with iOS, one of the biggest questions I had on these things was "Why?". Luckily posts like this, and the treasure trove of Mike Ash's blog provide a clear understanding of both how the components in Cocoa work, and why they work that way.

kurtle 12 years ago

"It relies on a relatively unusual API design – delegates..."

Nothing unusual about delegates, the entire iOS codebase is littered with them.

But kudos on doing what we all eventually think about doing, the longer we work with UITableView

yesimahuman 12 years ago

Funny, this was actually really helpful for me as I'm building a dynamic list (or "table" in iOS parlance) for HTML5 in Ionic Framework. Gave me a few ideas on how we could structure the API to handle tons of items efficiently. Great write up!

  • joshavant 12 years ago

    Thanks! Very glad to hear that!

    I suspected most of the concepts could be pretty easily applied to other languages, so I'm especially glad to hear this feedback.

  • jefflinwood 12 years ago

    Also check out how Android handles adapters for list views while you are at it - for both iOS and Android, you can efficiently recycle tagged views, so that if your table view has mixed types of data, you get the right recycled view.

perishabledave 12 years ago

Mike Ash did something similar on his Friday Q&A posts: http://www.mikeash.com/pyblog/friday-qa-2013-02-22-lets-buil...

  • ocrickard 12 years ago

    Yeah, I was thinking the same thing. Loved that blog post, and certainly enjoyed looking at R. Nystrom's implementation too: https://github.com/rnystrom/RNTableView.

    UITableView is one of those components that we all spend so much time with that everyone has little tweaks they want to make. Implementations like these at least give us options to extend/tweak low level behavior should we need to. I also love it when they write thorough blog posts like these explaining explicitly their rationale and structure of the component.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection