<?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>iPhone Programming Tutorials &#187; sqlite3</title>
	<atom:link href="/tag/sqlite3/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<item>
		<title>iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 4</title>
		<link>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/</link>
		<comments>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 23:47:39 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=392</guid>
		<description><![CDATA[This is the final installment of our 4 part series of creating a Todo list for the iPhone.  In this tutorial, I will detail how to add and delete new todo objects from the SQLite database.  Make sure that you have completed the following tutorials before you begin this one:

<a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a>
<a  ...]]></description>
				<content:encoded><![CDATA[<p style="text-align: left;">This is the final installment of our 4 part series of creating a Todo list for the iPhone.  In this tutorial, I will detail how to add and delete new todo objects from the SQLite database.  Make sure that you have completed the following tutorials before you begin this one:</p>
<ul style="text-align: left;">
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a></li>
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 2</a></li>
<li><a href="/2008/09/10/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-3/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 3</a></li>
</ul>
<p style="text-align: left;">When you have completed this tutorial, you should have a main screen that looks something like this:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/mainscreen1.png"><img class="size-full wp-image-394 aligncenter" title="screenshot" src="/wp-content/uploads/2008/09/mainscreen1.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Let&#8217;s get started&#8230;</p>
<p style="text-align: left;">The first thing we need to do is add the UIBarButtonItem items to the NavigationBar so that we get the &#8220;Edit&#8221; and &#8220;Add&#8221; button.  Open up <strong>RootViewController.m</strong> and add the following code to the <strong>viewDidLoad</strong> method.</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/viewdidload1.png"><img class="size-full wp-image-396 aligncenter" title="viewdidload" src="/wp-content/uploads/2008/09/viewdidload1.png" alt="" width="500" height="107" /></a></p>
<p style="text-align: left;">The first thing we see is the line that sets the leftBarButtonItem to self.editButtonItem.  This automatically adds the &#8220;Edit&#8221; button to the NavigationController.  Also, it sets up the functionality that allows the &#8220;delete&#8221; buttons to be displayed when the button is pressed.  You can see this functionality if you do a &#8220;Build and Go&#8221; at this step.  Next, I have manually created a UIBarButtonItem and added it to the navigationbar.  This can be done in Interface Builder, but I wanted to show you how to do it manually and assign an action to it (I&#8217;m sure you will require this functionality in a future program).  Here is a break down of the parameters:</p>
<ul style="text-align: left;">
<li>initWithTitle &#8211; The text to be displayed on the button</li>
<li>style &#8211; How the button will look</li>
<li>target &#8211; The class object that handles the messages sent from this button</li>
<li>action &#8211; The method to be called when the button is passed.  We can use @selector and give it the name of the function to call.</li>
</ul>
<p style="text-align: left;">Finally, we assign this button to the rightBarButtonItem.  If you do a Build and Go, it should error since we haven&#8217;t created the addTodo method. We will do that in a bit.  Now, let&#8217;s create a method inside of our Todo object that will add new Todos to the database.</p>
<p style="text-align: left;">Open up <strong>Todo.h</strong> and add the following code:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todo1.png"><img class="alignnone size-full wp-image-398" title="todo" src="/wp-content/uploads/2008/09/todo1.png" alt="" width="363" height="33" /></a></p>
<p style="text-align: left;">So in addition to the <strong>insertNewTodoIntoDatabase</strong> method, we also see the deleteFromDatabase method signature.  I have just added this so I don&#8217;t have to come back to it later.  We will be implementing this when I show you how to delete todos from the database.  One thing to note about the <strong>insertNewTodoIntoDatabase </strong>method is it has a &#8220;+&#8221; rather than a &#8220;-&#8221; sign.  This means that it is a static method.  Static methods are associated with the class not the instance meaning we can call this method without instanciating this class.  So we can do stuff like Todo.insertNewTodoIntoDatabase.  Now we will implement this method.</p>
<p style="text-align: left;">Before we can do this, we must declare a few more static sqlite3_statement&#8217;s.  Add the following statements to the top of <strong>Todo.m</strong></p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todomstatic1.png"><img class="size-full wp-image-400 aligncenter" title="todomstatic" src="/wp-content/uploads/2008/09/todomstatic1.png" alt="" width="270" height="30" /></a></p>
<p style="text-align: left;">Nothing new here&#8230;Now implement the following method:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todom31.png"><img class="size-full wp-image-399 aligncenter" title="todom3" src="/wp-content/uploads/2008/09/todom31.png" alt="" width="499" height="197" /></a></p>
<p style="text-align: left;">This is similar to our update method.  Notice that we are inserting default values into the database.  This is so we don&#8217;t run into any problems with null or nil values.  The most important part of this method is the fact that it returns the primary key of the newly created todo object.  This will be used later so we can immediately transition to the todo when the &#8220;Add&#8221; button is pressed.  The last thing we need to do to the todo object is update the dehydrate method so that the todoText gets saved if it gets changed.  Update the dehydrate method to look like this:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/dehydrate1.png"><img class="size-full wp-image-401 aligncenter" title="dehydrate" src="/wp-content/uploads/2008/09/dehydrate1.png" alt="" width="500" height="257" /></a></p>
<p style="text-align: left;">There are only a few minor changes here.  First we see the &#8220;text = ?&#8221; part added to the sql statement.  This is simply so we can update the text of the todo.  The other change is we bound the self.text property to the 1st question mark in the sql statement.  One thing to notice is we call [self.text UTF8String].  This is because sqlite3_bind_text takes a (char *).  This will convert an NSString to an acceptable format.</p>
<p style="text-align: left;">Now we need to add a method inside of our RootViewController to add a todo.  This is the method that will be called when the user presses the &#8220;Add&#8221; button.  Inside of RootViewController.m add the following code:</p>
<p><a href="/wp-content/uploads/2008/09/1-rootviewcontroller1.png"><img class="size-full wp-image-422 aligncenter" title="1-rootviewcontroller" src="/wp-content/uploads/2008/09/1-rootviewcontroller1.png" alt="" width="500" height="179" /></a></p>
<p style="text-align: left;">First, we get a reference to the appDelegate object.  This is because we need to call its addTodo method.  Next, we instantiate the TodoViewController if it has not already been instantiated.  We need this around because we will push it on to the view stack and transition to it after we create our new todo object.  After this is done, we call the addTodo method of the appDelegate.  It will return the newly created todo object and the view will be transitioned to its detail screen in order to update its details.  Now we need to implement the method addTodo inside of our appDelegate.  Open up todoAppDelegate.h and add the following code to create the method signature.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/2-appdelegateh1.png"><img class="size-full wp-image-423 aligncenter" title="2-appdelegateh" src="/wp-content/uploads/2008/09/2-appdelegateh1.png" alt="" width="107" height="18" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">Now, let&#8217;s implement this method.  Open up todoAppDelegate.m and add the following code:</p>
<p style="text-align: left;">
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/3-appdelegatem1.png"><img class="size-full wp-image-424 aligncenter" title="3-appdelegatem" src="/wp-content/uploads/2008/09/3-appdelegatem1.png" alt="" width="500" height="101" /></a></p>
<p style="text-align: left;">
<p>First, we are calling the insertNewTodoIntoDatabase method of the Todo object.  Notice that we are simply calling the method without first building an instance of a todo object.  As I said in tutorial 3, this is because that method is static and gets called without building an instance of the class.  Next, we insatiate the todo object that was just created by calling its initWithPrimaryKey method.  This will give us reference to the new todo object.  Finally, we append this todo to the end of our todos array.  Since our UITableView is updated with this array, it will automatically include the new todo object.  The last line just returns this todo object.</p>
<p>Remember is the last tutorial we made it so the users could update the status and the priority of a todo?  Well, now we also need to give them the ability to update the text of the todo.  So open up TodoViewController.h and add the following code:</p>
<p style="text-align: center;">
<p><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png"><img class="alignnone size-full wp-image-425" title="5-todoviewcontrollerh" src="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png" alt="" width="430" height="271" /></a><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png"><br />
</a></p>
<p style="text-align: left;">
<p>Ok, so I&#8217;m guessing you are wondering why the UITextView for the todoText object has been changed to a UITextField.  Well, I will tell you.  UITextView doesn&#8217;t have the methods that we need to save the text with our current design.  We will also be changing this on our Interface inside of Interface Builder.  So for now, just believe me and anywhere it says UITextView, change it to UITextField.  The only additional code we added here is the method signature for the updateText method.  It&#8217;s an IBAction that will get called when the user presses the &#8220;Done&#8221; button on the keyboard after setting the text for the todo.  Next, we need to implement this method.  Open up TodoViewController.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerm1.png"><img class="size-full wp-image-426 aligncenter" title="5-todoviewcontrollerm" src="/wp-content/uploads/2008/09/5-todoviewcontrollerm1.png" alt="" width="245" height="53" /></a></p>
<p style="text-align: left;">All this does is update the text of the todo to the text that the user entered inside of the UITextField.  The last thing we need to do in order to add a todo is to replace the UITextView with a UITextField and connect it to our updateText method.  Double click on your TodoViewController.xib file to open it in Interface Builder.</p>
<p>Now click on the UITextView on your interface and press the delete key on your keyboard to delete it.  Now, drag a UITextField from the library and drop it onto your interface.  Resize it to fit.  When you have completed that, your interface should look something like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/6-interfacebuilder1.png"><img class="size-full wp-image-427 aligncenter" title="6-interfacebuilder" src="/wp-content/uploads/2008/09/6-interfacebuilder1.png" alt="" width="320" height="502" /></a></p>
<p style="text-align: left;">Now we need to connect this component.  Make sure it is selected and click Tools -&gt; Connections Inspector to open up the connections inspector.  Drag from the circle next to the method &#8220;Did End On Exit&#8221; to the &#8220;File&#8217;s Owner&#8221; object.  The words udpateText should pop up.  Click on them to make the connection.  Next, click in the circle next to &#8220;New Referencing Outlet&#8221; and drag it to the &#8220;File&#8217;s Owner&#8221; object.  Select todoText  when it pops up.  The Connections Inspector should look like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/12-ib1.png"><img class="size-full wp-image-434 aligncenter" title="12-ib" src="/wp-content/uploads/2008/09/12-ib1.png" alt="" width="287" height="708" /></a></p>
<p>Now we are done with Interface Builder.  Go ahead and close it.  We are now able to add todos.  The last thing we need to do is give the ability to delete todos from the list as well as our database.  This is all done in code, and we won&#8217;t need interface builder for this.</p>
<p>Let&#8217;s start by adding the methods to the appDelegate to handle the deletion of todos.  Open up todoAppDelegate.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/7-appdelegateh1.png"><img class="alignnone size-full wp-image-428" title="7-appdelegateh" src="/wp-content/uploads/2008/09/7-appdelegateh1.png" alt="" width="194" height="16" /></a></p>
<p style="text-align: left;">All we see here is a signature for the removeTodo method.  Also, be sure to add a #import &#8220;Todo.h&#8221; statement to the top of this file so that we can interface with the todo objects. Now let&#8217;s implement the removeTodo method.  Open up todoAppDelegate.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/8-appdelegatem1.png"><img class="size-full wp-image-429 aligncenter" title="8-appdelegatem" src="/wp-content/uploads/2008/09/8-appdelegatem1.png" alt="" width="308" height="119" /></a></p>
<p style="text-align: left;">The first line looks up the todo in the todos NSArray.  It returns the index in the array of the todo to be deleted.  Then, we call the deleteFromDatabase method on the todo object and then remove it from the todos array.  Since the UITableView is updated via this array, it will automatically remove the todo without any additional code on our part.</p>
<p>Now, let&#8217;s create the removeTodo method for the todo object. We have already written the method signature in Todo.h in a previous step, so open up Todo.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/10-todom1.png"><img class="size-full wp-image-431 aligncenter" title="10-todom" src="/wp-content/uploads/2008/09/10-todom1.png" alt="" width="500" height="182" /></a></p>
<p style="text-align: left;">Remember the delete_statement variable is a static sqlite3_stmt that we declared in a previous step.  First, we check to see if it is nil.  If it is we compile the statement using the sqlite3_prepare statement.  Next, we bind the primary key of the current todo to the &#8220;?&#8221; in the sqlite3 statement.  Next, we just step the statement to execute it and reset it.  The last thing we need to do to delete todos from the database is to specify what happens when the user presses the &#8220;delete&#8221; button.  Open up RootViewController.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/11-rootviewcontroller11.png"><img class="size-full wp-image-432 aligncenter" title="11-rootviewcontroller1" src="/wp-content/uploads/2008/09/11-rootviewcontroller11.png" alt="" width="499" height="143" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">The first step (like the first step of many functions) is to get a reference to the appDelegate.  Next, we check to see if we are currently editing.  If so, call the removeTodo method on appDelegate.  The next line, removes the row from the UITableView at the given indexPath.</p>
<p>Now click Build and Go!  You should now be able to add and delete todo items from the database.  This concludes our four part series of creating a todo list for the iPhone.  As always, if you have any questions feel free to leave them in the comments section.  If you get lost at any time you can download the sample code <a href="/wp-content/uploads/2008/09/todo-part-41.zip">here</a>.</p>
<p>Happy iCoding!</p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/feed/</wfw:commentRss>
		<slash:comments>115</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 2</title>
		<link>http://icodeblog.com/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/</link>
		<comments>http://icodeblog.com/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 17:35:46 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=293</guid>
		<description><![CDATA[
This tutorial is part 2 in our series of creating a to-do list.  I will assume that you have completed the following tutorial and its prequisites.

<a href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a>

I will be using the code produced from that tutorial as a base for this one.  When you are finished with this tutorial, your application will look something like this:
<a href="/wp-content/uploads/2008/09/screenshot2.png"></a>
In this section, I will not only teach you how to  ...]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript"><!--
digg_url = 'http://digg.com/programming/iPhone_Programming_Tutorial_Creating_a_ToDo_List_Using_SQL';
// --></script><script src="http://digg.com/api/diggthis.js"></script></p>
<p>This tutorial is part 2 in our series of creating a to-do list.  I will assume that you have completed the following tutorial and its prequisites.</p>
<ul>
<li><a href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a></li>
</ul>
<p>I will be using the code produced from that tutorial as a base for this one.  When you are finished with this tutorial, your application will look something like this:</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/09/screenshot2.png"><img class="alignnone size-full wp-image-327" title="screenshot" src="/wp-content/uploads/2008/09/screenshot2.png" alt="" width="386" height="742" /></a></p>
<p>In this section, I will not only teach you how to display the SQL data in UITablewView, but I will be detailing how to display it in <strong>multiple columns</strong> with images and text.  For this tutorial, you will need to download the following images.</p>
<ul>
<li><a href="/wp-content/uploads/2008/09/red1.png"><img class="alignnone size-full wp-image-304" title="red1" src="/wp-content/uploads/2008/09/red1.png" alt="" /></a></li>
<li><a href="/wp-content/uploads/2008/09/green1.png"><img class="alignnone size-full wp-image-305" title="green1" src="/wp-content/uploads/2008/09/green1.png" alt="" /></a></li>
<li><a href="/wp-content/uploads/2008/09/yellow1.png"><img class="alignnone size-full wp-image-306" title="yellow" src="/wp-content/uploads/2008/09/yellow1.png" alt="" /></a></li>
</ul>
<p>We will be using these images to denote the priority (Green = low, Yellow = medium, Red = high).</p>
<h2>Bring Your Code Up To Speed</h2>
<p>Before we begin, we need to add some code to the Todo.h and Todo.m class to support the priority field in the database.  Open up Todo.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todoh2.png"><img class="size-full wp-image-308 aligncenter" title="todoh" src="/wp-content/uploads/2008/09/todoh2.png" alt="" width="378" height="146" /></a></p>
<p style="text-align: left;">All that is new here is the added NSInteger priority property.  We will be using this to get and set the priority for a given todo object.  Next, open Todo.m and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todom12.png"><img class="size-full wp-image-310 aligncenter" title="todom1" src="/wp-content/uploads/2008/09/todom12.png" alt="" width="499" height="326" /></a></p>
<p style="text-align: left;">The first line that has changed is the synthesize line.  We added our priority property to allow XCode to create the getter and setter methods for it.  Next, you will notice that the sql statement has changed slightly.  We are now getting the priority in addition to the text from the todo table.  Finally,  we set self.priority property to the selected priority value from the todo table.  This is done by using the sqlite3_column_int method.  We pass the init_statement and the number 1.  1 being the index of the sql array for which the priority data is contained.</p>
<h2>Add Images to Your Project</h2>
<p>Download the images above and save them to your project directory.  Inside of your project, right click (control-click) on the <strong>Resources</strong> folder and click <strong>Add -&gt; Existing Files&#8230;</strong> Browser for the images, select all of them and click <strong>Add</strong>. Check the box that sais &#8220;Copy items into destination group&#8217;s folder (if needed)&#8221;.  Click <strong>Add. </strong>The image files should now appear inside of your <strong>Resources </strong>folder.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/add-images1.png"><img class="size-full wp-image-300 aligncenter" title="add-images" src="/wp-content/uploads/2008/09/add-images1.png" alt="" width="400" height="374" /></a></p>
<h2>Create a UITableViewCell Subclass</h2>
<p>To display data in columns within a UITableView, we have to create our own cell class that defines the type of data we want to display.  By default, Apple provides us with a simple cell object that can only display one column of text.  Normally, this is fine as it will work for a wide variety of applications.  Since we require 3 columns for this tutorial, we need to wrap our own cell object.</p>
<p>Click <strong>File -&gt; New File&#8230;</strong> and select <strong>UITableViewCell</strong>. Click <strong>Next.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/uitableviewcell1.png"><img class="size-full wp-image-295 aligncenter" title="uitableviewcell" src="/wp-content/uploads/2008/09/uitableviewcell1.png" alt="" width="500" height="368" /></a></p>
<p>Name this file <strong>TodoCell</strong> and make sure this that the box that sais &#8220;Also create TodoCell.h&#8221; is checked.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocell1.png"><img class="size-full wp-image-296 aligncenter" title="todocell" src="/wp-content/uploads/2008/09/todocell1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">This will create a &#8220;barebones&#8221; UITableViewCell object with some basic methods already filled out.  Let&#8217;s add some properties to this class.  Open up <strong>TodoCell.h</strong> and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellh1.png"><img class="alignnone size-full wp-image-313" title="todocellh" src="/wp-content/uploads/2008/09/todocellh1.png" alt="" width="403" height="292" /></a></p>
<p style="text-align: left;">Let&#8217;s take this line by line&#8230;</p>
<p style="text-align: left;">First, we see a Todo object being declared.  Each cell will know which Todo item is associated with it.  This will help out when updating the data in each cell.  Next, we see 2 UILabels and a UIImageView.  To understand why these components are needed, here is a screenshot of how each cell will look.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/datasample1.png"><img class="alignnone size-full wp-image-314" title="datasample" src="/wp-content/uploads/2008/09/datasample1.png" alt="" width="320" height="45" /></a></p>
<p style="text-align: left;">We see the &#8220;Green Dot&#8221; which is an image being rendered by a UIImageView.  The word &#8220;low&#8221; and &#8220;Take out the trash&#8221; are both UILabels. After they are declared, we simply create them as properties.  Notice that we are NOT creating a property for the Todo object. We will not be synthesizing it either. This is because we want this variable to be private.  Setting this variable requires some additional code so we don&#8217;t want any code writer to simply be able to say cell.todo = foo;  You will see why this is so further on in this tutorial.</p>
<p style="text-align: left;">Below this are some method declarations.  First we see the method &#8220;imageForPriority&#8221;.  We will be using this method to decide which image (green, red, yellow) gets displayed for a given priority.  Next, we see the &#8220;getter and setter&#8221; methods for the todo object.  As I explained above, the setter will contain additonal code besides assigning the todo object.</p>
<p style="text-align: left;">Now open up <strong>TodoCell.m</strong>.  We will be writing quite a bit of code in here so I will break it up the best I can.  First, add the following code to create some of the initialization:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm11.png"><img class="alignnone size-full wp-image-316" title="todocellm1" src="/wp-content/uploads/2008/09/todocellm11.png" alt="" width="485" height="344" /></a></p>
<p style="text-align: left;">Ok, some new stuff here.  First, we see 3 static UIImages.  These will hold reference to each of the three images (red, green, yellow).  Since we only need to allocate them once, we make them static.  Static means that they will be associated with the class not the instance.  So we can make as many TodoCells as we want but only 3 UIImages will be created.  On the next line there is a private interface.  This allows us to declare a private method that no one else can use except this class.  Following this is the synthesize line.  Notice again that we are NOT synthesizing the todo object.</p>
<p style="text-align: left;">Looking at the initialize method&#8230;  All that is going on here is we are intanciating each of our UIImages with the correct image for a given priority.  This initialize method will get called once when the first instance of the todocell class is built.  Moving on&#8230; Add the following code: (Note: it might be small and hard to read.  If this is the case, click on the image to open it and the text will be full size)</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm21.png"><img class="size-full wp-image-317 aligncenter" title="todocellm2" src="/wp-content/uploads/2008/09/todocellm21.png" alt="" width="500" height="292" /></a></p>
<p style="text-align: left;">This is the initialiazation method for any UITableViewCell.  First, we need to call the super classe&#8217;s (UITableViewCell) initWithFrame to ensure that the underlying components of the cell get set up properly.  Next, we get a reference to the contentView.  The contentView is the view for each cell.  We will be adding all of our UI components to this view.</p>
<p style="text-align: left;">The next 3 lines initialize a UIImageView and add it to our view.  Notice that we are populating it with the priority1Image.  This will just be a dummy placeholder until we update it.</p>
<p style="text-align: left;">Following this, we initialize the todoTextLabel.  This label will display what it is we need &#8220;to do&#8221; such as &#8220;Take out the trash&#8221;.  There is a method that we will be calling called &#8220;newLabelWithPrimaryColor&#8221;.  This is a method I will detail a little further down.  What it will do is build a new label with the attributes that we specify when we call it.  This method was taken directly from Apple&#8217;s &#8220;Seismic XML&#8221; sample code.  It&#8217;s pretty handy.  After this gets called, we simply add the new label to our view and these steps get repeated for the todoPriorityLabel.</p>
<p style="text-align: left;">Finally, the method &#8220;bringSubviewToFront&#8221; is called on the priority UIImageView.  This method is used in case there is text that gets near the image.  It will cause the image to appear above the text.  <strong>You can use this for layering your UI components.</strong></p>
<p style="text-align: left;">Still with me?  Good&#8230; now let&#8217;s add the following &#8220;getter&#8221; and &#8220;setter&#8221; methods for the todo object.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm31.png"><img class="size-full wp-image-318 aligncenter" title="todocellm3" src="/wp-content/uploads/2008/09/todocellm31.png" alt="" width="492" height="388" /></a></p>
<p style="text-align: left;">The first method todo is simple.  All it does is return our todo object.  The setTodo is a little more involved&#8230;</p>
<p style="text-align: left;">First, we set the incoming (newTodo) to our classe&#8217;s todo object.  Next, we update the UITextLabel so we can display the detailed todo information.  Following this we set the image of our UIImageView by calling the method imageforPriority.  I will detail this method further down in this tutorial but all it does is return an image for a given priority.  Last, we have a switch statement.  The syntax of a switch statement is the same in objective C as it is in most languages.  If you don&#8217;t know what a switch statement is <a href="switch statement" target="_blank">Google it</a>.  Based on the priority of the newTodo, the priority label gets updated with one of three words (High, Medium, Low).  The [self setNeedsDisplay] tells the cell to redisplay itself after this todo has been set.</p>
<p style="text-align: left;">Now, let&#8217;s add the code that lays out the cell.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm41.png"><img class="size-full wp-image-319 aligncenter" title="todocellm4" src="/wp-content/uploads/2008/09/todocellm41.png" alt="" width="500" height="432" /></a></p>
<p style="text-align: left;">This method gets called automatically when a UITableViewCell is being displayed.  It tells the UITableView how to display your cell.  The define statements are similar to define statements in C.  The reason we are coding like this is because we can tweak these variables to get the display to our liking.  First, we call the layoutSubviews of the super class.  Next, we get a reference to the contentView.bounds.  This variable will allow us to figure out how much drawing area we have and allow us to line objects up properly.</p>
<p style="text-align: left;">The if(!self.editing) part is not neccessary but is good practice.  You would use this if you allowed editing of your cells.  This code is a little tough to explain by typing, but I will do the best that I can.  First, we declare our right-most column.  This is done by making a frame to hold the content.  This column will hold the text of the todo item.  Most of the code here is just positioning.  You can play with these numbers and see how it moves stuff around.  Once all of the positioning code is completed, the frame of our todoTextLabel gets set to this newly created frame.  This is done for each of our UI components.  You can lay them out however you like, as I may not have the best layout.</p>
<p>We have one more method to override.  It&#8217;s the setSelected method.  Go ahead and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm51.png"><img class="size-full wp-image-321 aligncenter" title="todocellm5" src="/wp-content/uploads/2008/09/todocellm51.png" alt="" width="380" height="276" /></a></p>
<p style="text-align: left;">This method gets called when the user taps on a given cell.  We need to tell the cell how to behave when it gets tapped on.  This method should look pretty straight forward.  First, we call the setSelected method of the super class.  Next, we update the background color depending on whether or not the cell was selected.  Finally, the labels get set to a white color if the cell gets selected.  This is to contrast the blue color that the background becomes when the cell is selected.</p>
<p style="text-align: left;">This last 2 methods that I want to talk about are the helper methods that we used earlier in the code.  Add the following methods to your code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm61.png"><img class="size-full wp-image-322 aligncenter" title="todocellm6" src="/wp-content/uploads/2008/09/todocellm61.png" alt="" width="500" height="402" /></a></p>
<p style="text-align: left;"><strong>newLabelWithPrimaryColor</strong></p>
<p>This method got called when we were initializing our UILabels.  It takes a few parameters that should be pretty self explanatory.  Looking through the code, we first see the font being initialized with the size that we specified.  If bold was specified this is also accounted for.  Next, we instantiate a new UILabel and give it some properties.  Finally, this newly created UILabel gets returned.</p>
<p><strong><span style="color: #888888;">imageForPriority</span></strong></p>
<p>This method is actually quite simple.  It simply takes a priority and returns the UIImage that is associated with that priority.  Notice the default clause.  I decided to handle it like this instead of doing &#8220;case 1&#8243; to handle all other cases.  For whatever reason, if there is ever a priority that is not 1,2 or 3 it will, by default, have low priority.</p>
<p>Now that we have created our UITableViewCell, we need to display it in the table.  Open up RootViewController.m and add the following import statement.  This will allow us to use our TodoCell object.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/rootviewcontrollerm11.png"><img class="size-full wp-image-323 aligncenter" title="rootviewcontrollerm1" src="/wp-content/uploads/2008/09/rootviewcontrollerm11.png" alt="" width="130" height="18" /></a></p>
<p style="text-align: left;">Now find the numberOfRowsInSection method and add the following code</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/rootviewcontrollerm21.png"><img class="size-full wp-image-324 aligncenter" title="rootviewcontrollerm2" src="/wp-content/uploads/2008/09/rootviewcontrollerm21.png" alt="" width="500" height="52" /></a></p>
<p style="text-align: left;">I&#8217;m not going to really go over this, as this is almost the exact same code as in the Fruits example.  Basically, we are returning the number of todo items.</p>
<p>Now for the magic&#8230;We will now add our TodoCell to allow it to be displayed.  Find the cellForRowAtIndexPath method and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/rootviewcontrollerm31.png"><img class="size-full wp-image-325 aligncenter" title="rootviewcontrollerm3" src="/wp-content/uploads/2008/09/rootviewcontrollerm31.png" alt="" width="500" height="194" /></a></p>
<p style="text-align: left;">This code is fairly similar to the default code that Apple has provided us.  The first change is we are instantiating our TodoCell object.  We are creating it with the initWithFrame method and passing our identifier to it.  Next, we get reference to the application&#8217;s appDelegate and use it to look up the todo item at the given index.  This should be familiar.  Finally, we set the todo item of the cell to the todo item at the row index and return the cell.  That&#8217;s it! Go ahead and click the Build and Go icon and see your todo list come to life.  Here is a screenshot of what your app should look like.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/screenshot2.png"><img class="alignnone size-full wp-image-327" title="screenshot" src="/wp-content/uploads/2008/09/screenshot2.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">That concludes part 2 of this tutorial. Join me next time as I show you how to display detailed todo info using some new UI controls that we haven&#8217;t seen yet. As always, post your questions and comments in the comments section of the blog. <a href="/wp-content/uploads/2008/09/todo-part-211.zip">Download The Sample Code</a></p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/feed/</wfw:commentRss>
		<slash:comments>111</slash:comments>
		</item>
	</channel>
</rss>
