<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Little Green Software &#187; Programming</title>
	<atom:link href="http://littlegreensoftware.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://littlegreensoftware.com</link>
	<description>Custom Apps for Smart Phones</description>
	<lastBuildDate>Wed, 21 Jul 2010 14:35:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>Document-based Dynamic Table Views</title>
		<link>http://littlegreensoftware.com/document-based-dynamic-table-views/</link>
		<comments>http://littlegreensoftware.com/document-based-dynamic-table-views/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 20:17:36 +0000</pubDate>
		<dc:creator>Ed</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://littlegreensoftware.com/?p=140</guid>
		<description><![CDATA[The iPhone application I'm developing uses a Property List as the fundamental data structure for all content in the application. In this article I discuss my solution for viewing and editing the plist document in an attractive, easily customizable format.]]></description>
			<content:encoded><![CDATA[<p>The iPhone application I&#8217;m developing uses a <acronym title="A Property List is a type of XML document that supports representing all of the basic Core Foundation data structures (arrays, dictionaries, strings, numbers, etc). All of those data structures have built in methods to serialize to or from a Property List (plist), making plists a powerful and extremely convenient mechanism for managing data. See the Property List Programming Guide for more information.">Property List (plist)</acronym> as the fundamental data structure for all app content. The application&#8217;s job is to present the plist document to the user in an attractive format, and to allow editing of the document.</p>
<p>Apple has already solved this problem; the <acronym title="See The Settings Bundle documentation for more information.">Settings Bundle</acronym> in an iPhone application is a plist file. The Settings app built into the iPhone OS displays a hierarchy of views that allow you to view and edit this Settings Bundle. I imagine one could gain some good insight if Apple were to release the source code for the Settings.app, or a sample based on that application. Meanwhile, we are on our own.</p>
<p>When I first began thinking about a good design for this application I thought of XSL style sheets. An XSL style sheet turns an XML document into a visual representation, typically an HTML document rendered by a browser. I desired an analogous system for my app. The style sheet concept is an ideal worth striving for, but as a model it is slightly too simplistic because there&#8217;s no good equivalent to the browser in the analogy. So instead of translating the document into another document, we will translate the document into code that defines a view.</p>
<p>This conceptual model can be applied to any view, but I chose to use a hierarchy of table views because table views — especially the &#8220;grouped&#8221; style table view — provide a good basic structure to the view.  A table view is comprised of an array of sections, and each section has an array of rows. I defined a <code>TableSection</code> class to represent a table section; the table view data source is an array of <code>TableSection</code> instances.  For each <code>UITableViewDelegate</code> and <code>UITableViewDatasource</code> method that defines behavior for a specified section, we use the section argument to look up the corresponding <code>TableSection</code> instance, and delegate the implementation to that instance.</p>
<blockquote><p><code>- (UITableViewCell *)tableView:(UITableView *)aTableView<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cellForRowAtIndexPath:(NSIndexPath *)indexPath<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;TableSection* tableSection = [tableSections objectAtIndex:indexPath.section];<br />
&nbsp;&nbsp;&nbsp;&nbsp;return [tableSection tableView:aTableView cellForRow:indexPath.row];<br />
}</code></p></blockquote>
<p>The pattern is identical for methods such as <code>numberOfRowsInSection</code>, <code>heightForRowAtIndexPath</code>, <code>canEditRowAtIndexPath</code>, and so forth. Thus the behavior is in the hands of the <code>TableSection</code> (or derived class) instance. The instance holds a reference to the view controller and document, from which it can draw whatever data it needs, and to which it can write any changes it desires.</p>
<p>Not every <code>UITableViewDelegate</code> method fits nicely into this paradigm. For example, it is typically easier to implement  the majority of <code>didSelectRowAtIndexPath</code> directly in the view controller class. To facilitate identification of the  <code>TableSection</code> instance a user has selected, I added an integer tag property to <code>TableSection</code>, identical to the tag property in the <code>UIView</code> class.</p>
<p>With the combination of the delegation of some methods and use of the tag property in other methods, we now have a very flexible document-based view setup. The power of this setup became clear once I began developing a library of <code>TableSection</code> subclasses.</p>
<ul>
<li><strong><code>ButtonTableSection</code></strong> implements a basic clickable button. When the row is selected, it is treated as as a button-press. The action is implemented in the <code>didSelectRowAtIndexPath</code> method; the button may be identified by the section tag.</li>
<li><strong><code>MultirowTableSection</code></strong> indexes into an array object in the document instead of of drawing data directly from root level of the document as the <code>TableSection</code> base class does.</li>
<li><strong><code>CheckboxTableSection</code></strong> allows toggling of a set of non-exclusive boolean options in the document.</li>
<li><strong><code>RadioTableSection</code></strong> allows setting a variable to one of several allowed values.</li>
</ul>
<p>For the <code>UITableViewDatasource</code> delegate methods that define the appearance of a particular cell, the <code>TableSection</code> delegates the behavior yet again to a cell class, <code>TableCell</code>, derived from <code>UITableViewCell</code>.  <code>TableCell</code> makes use of a <a href="http://www.llamagraphics.com/drupal/blogs/samalone/2008/10/01/iphone-dev-simplifying-table-cell-creation">technique described on the Llamagraphics blog</a> that generalizes the logic for obtaining an instance of the cell class. <code>TableCell</code> also defines a method called <code>setupCellForDocument</code> that is responsible for configuring the cell instance with the data in the specified document.</p>
<p>Once I announce more details of my application I will post some screenshots. For now I hope other developers find this design inspires insight for your own document-based apps. I look forward to your feedback!</p>
]]></content:encoded>
			<wfw:commentRss>http://littlegreensoftware.com/document-based-dynamic-table-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
