Skip to content

Tables and Grids Guide

Matt Greenfield edited this page Aug 15, 2014 · 12 revisions

MGBoxKit provides layout engines for tables and grids and two content providing APIs, one suitable for small tables and one suitable for large. The small table API is quick and easy to use and most suitable for static tables or dynamic tables with small data sets. For larger data sets it's most appropriate to use the dynamic API which supports box reuse and offscreen culling.

An example of a small table might be your app's settings view, with rows for changing various preferences, account management, etc. An example of a large table might be your app's main view that shows a long, dynamic list of items fetched from a server. Think something like Facebook's newsfeed or a Twitter stream.

The reason for two separate APIs should be recognisable to anyone who has tried to use UITableView for a small table and found it overly awkward and troublesome for something that should be simple and quick. For many tables you don't need all that API bloat, and it just gets in the way.

The large table API also attempts to be easier and less cumbersome than the UITableView / UICollectionView equivalents, though given the need for box reuse and offscreen culling, most of the same requirements exist and the concepts should feel familiar.

Basics

All tables and grids require an MGBox or MGScrollView container and MGLayoutBox content items (eg MGBox, MGLine). Think of it as similar to creating a UIScrollView and adding subviews, with the main difference being that you add the content items to [boxes](-[MGLayoutBox boxes]) instead of to subviews.

Scrollable Container

MGScrollView *scroller = [MGScrollView scrollerWithSize:self.view.size];
[self.view addSubview:scroller];

Not Scrollable Container

MGBox *container = [MGBox boxWithSize:self.view.size];
[self.view addSubview:container];

Small Table API

For small static tables and grids the easiest approach is to create and add box objects to a container's [boxes](-[MGLayoutBox boxes]) array, then call the container's [layout](-[MGLayoutBox layout]) or [layoutWithSpeed:completion:](-[MGScrollView layoutWithSpeed:completion:]) method.

Large Table API

For tables and grids of dynamic content you can use the same technique as for static content, or optionally use the boxProvider approach. The choice of which approach to use should be based on how large you expect the data set to be. For large data sets or memory heavy boxes (eg boxes with many subviews) you should use the boxProvider approach, which supports box reuse and offscreen culling. This is similar to the approaches taken by UITableView and UICollectionView.

Clone this wiki locally