<?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>RyanJD</title>
	<atom:link href="http://ryanjd.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryanjd.com</link>
	<description>Programming</description>
	<lastBuildDate>Fri, 11 Jun 2010 10:44:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Archiving data</title>
		<link>http://ryanjd.com/index.php/archiving-data/</link>
		<comments>http://ryanjd.com/index.php/archiving-data/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 14:10:18 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=158</guid>
		<description><![CDATA[Quick thoughts as to archiving data: The best method seems to be saving to the /Caches/ directory of the iPhone application package. Then, to support the new Documents feature is easy &#8211; just move a file from /Caches/ to /Documents/ and the file is exported. As for importing, do the opposite, and effectively force a [...]]]></description>
			<content:encoded><![CDATA[<p>Quick thoughts as to archiving data:<br />
The best method seems to be saving to the /Caches/ directory of the iPhone application package. Then, to support the new Documents feature is easy &#8211; just move a file from /Caches/ to /Documents/ and the file is exported. As for importing, do the opposite, and effectively force a &#8216;restart&#8217; of the application (by forcing all managers and singletons to reload their data).</p>
<p>The problem with this is the process of saving inside the program. Should it be done every time the stored data is modified? But then, what happens when the data is modified 100+ times per second? The application would attempt to encode the data, and write it to the device 100 times per second. The iPhone would simply crash in this situation (on the simulator it would even be slow! this is an extreme example). Another solution is to provide a superclass to data managing objects, which could control a batch-flow workspace. They could store a boolean as to whether the data should be saved each step or not. Then the saving would look like this:</p>
<pre>Disable saving
Run loop - modify a bunch of data
Enable saving (this would also save the data)</pre>
<p>By default, saving should be switched on. Personally, I&#8217;d name the methods something like as follows:</p>
<pre>[dataObject beginBatchModifications];
for (unsigned i = 0; i < 100; i ++) {
    [dataObject removeObjectAtIndex:i];
}
[dataObject finishBatchModificationsAndSave];</pre>
<p>This is not a perfect solution, but provides a neat code when finished.</pre>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/archiving-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>UITableView reloadData not working!</title>
		<link>http://ryanjd.com/index.php/uitableview-reloaddata-not-working/</link>
		<comments>http://ryanjd.com/index.php/uitableview-reloaddata-not-working/#comments</comments>
		<pubDate>Sat, 29 May 2010 16:24:38 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[iPhone Development]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=155</guid>
		<description><![CDATA[For all of you people that are having this problem (as I was just experiencing) check a few things before submitting a bug report to Apple: If you are using Interface Builder to set up your custom UITableView (without using a UITableViewController), then check that your IBOutlets are set up properly for UITableView&#8217;s properties: delegate [...]]]></description>
			<content:encoded><![CDATA[<p>For all of you people that are having this problem (as I was just experiencing) check a few things before submitting a bug report to Apple:</p>
<ul>
<li>If you are using Interface Builder to set up your custom UITableView (without using a UITableViewController), then check that your IBOutlets are set up properly for UITableView&#8217;s properties: delegate and dataSource. If they are <strong>nil<em> </em><span style="font-weight: normal;">then your reloadData call will not work. Then again, your entire table won&#8217;t work, so it&#8217;s pretty clear if you haven&#8217;t set them.</span></strong></li>
<li>If you have subclassed UITableViewController to run your custom processes and show your custom design, then make sure you have not set the tableView property. If you have, it will override UITableViewController&#8217;s original tableView property and will ruin your application (and will most likely crash it).</li>
<li>You must call reloadData <strong>on the main thread.</strong> This was my problem. If you are doing the sensible thing and downloading some data on a separate thread, and then passing it back through delegate methods or a notification center to your custom UITableViewController, make sure that you call reloadData back on the main thread. If you&#8217;re having trouble with this, use this or similar code:</li>
</ul>
<pre>[[self tableView] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];</pre>
<p>Hopefully this cures some headaches.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/uitableview-reloaddata-not-working/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Login via MySQL</title>
		<link>http://ryanjd.com/index.php/login-via-mysql/</link>
		<comments>http://ryanjd.com/index.php/login-via-mysql/#comments</comments>
		<pubDate>Sat, 01 May 2010 10:53:33 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[username]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=150</guid>
		<description><![CDATA[It seems not many people know how to do this, or they spend a long time figuring it out. I figured I might as well post it up for all to see. This tutorial will teach you to set up a mysql table in a currently existing database, and use that mysql table to store [...]]]></description>
			<content:encoded><![CDATA[<p>It seems not many people know how to do this, or they spend a long time figuring it out. I figured I might as well post it up for all to see.</p>
<p>This tutorial will teach you to set up a mysql table in a currently existing database, and use that mysql table to store usernames and passwords of your members. It will then show you how to verify that a user with a specific username and password exists.</p>
<p>This tutorial uses PHP, MySQL queries, and some HTML to achieve this.</p>
<p>Some important things to keep in mind are:</p>
<ul>
<li>The database <strong>never</strong> keeps track of an unencrypted member password.</li>
<li>Members passwords must be case-sensitive, but usernames are optionally case-sensitive.</li>
</ul>
<p>We will be storing the members&#8217; passwords using the encryption method MD5. It is secure and easy to access in PHP.</p>
<p>So, first is to set up the table. I am presuming here that you already have a MySQL database set up. If you don&#8217;t, Google it.<br />
<span id="more-150"></span></p>
<p>We need to know the columns we will be using to specify our table. We need to store the members ID, the members username, and the members password. This is the bare minimum, and you can add your custom fields afterwards. A MySQL query should do the trick (you can run this from your database).</p>
<pre>CREATE TABLE tblMembers(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(32) NOT NULL,
password VARCHAR(32) NOT NULL)</pre>
<p>Feel free to insert a few test members.</p>
<p>The idea is this: we will store a password on the database as an MD5 hash (which is a case sensitive encryption) and we will encrypt the password being used as a login attempt to verify this password once we&#8217;ve found a matching username. If there is no matching username, then the user does not exist. If the password does not match the matched username, then the password is incorrect. If the password matches the username, then the verification is a success.<br />
The PHP code needs to do all of the above. Below is a script that does so (but you can&#8217;t just copy and paste!).</p>
<pre>&lt;?

/*
 * connect to mysql
 * select database
 * etc
 */

// error codes
define('__SUCCESS', true);
define('__ERRNUM_INVALIDUSERNAME', 100);
define('__ERRNUM_INCORRECTPASSWORD', 101);

function verify_credentials( $username, $password, $password_encrypted = FALSE,  &amp;$user = NULL)
{
        // retrieve members if their usernames match
        // lower() forces the username to lower case
        // and so the comparison is case insensitive
        $query =        mysql_query(
                                        sprintf(
                                                "SELECT username, password FROM tblMembers WHERE lower(username) = '%s';",
                                                mysql_real_escape_string(strtolower($username))
                                        )
                                );

        // if the query has no rows, it means the username does not exist
        if (mysql_num_rows($query) == 0)
        {
                // return the error code for 'invalid username'
                return __ERRNUM_INVALIDUSERNAME;
        }

        // if password is encrypted, don't encrypt again!
        // if password isn't encrypt it, encrypt it!
        $encrypted = ($password_encrypted) ? $password : md5($password);

        // loop through the found members to find a matching password
        while ($row = mysql_fetch_array($query))
        {
                // now we can access username and password of this row
                // the username is irrelevant - it MUST match or it shouldn't
                //  have been found by the query
                // so focus on the password
                if ($encrypted == $row['password'])
                {
                        // matching password!
                        // fill user if it was set
                        if (NULL != $user)
                        {
                                // give username and password to user
                                $user['username'] = $row['username'];
                                $user['password'] = $row['password'];
                        }

                        // report verification success
                        return __SUCCESS;
                }
        }

        // if we're still in this scope, it means the password wasn't found
        // so the password must have been incorrect
        return __ERRNUM_INCORRECTPASSWORD;
}

/*
 * close connection
 */

?&gt;</pre>
<p>You can call this function as follows:</p>
<pre>$username = "Username";
$password = "password";
echo "Attempting login with username {$username} and password {$password}&lt;br/&gt;";
$result = verify_credentials($username, $password, FALSE, &amp;$user);
if ($result == __SUCCESS)
{
        echo "Login successful. User found with credentials: {$user['username']} : {$user['password']}&lt;br/&gt;";
} else if ($result == __ERRNUM_INVALIDUSERNAME)
{
        echo "Login failed. User could not be found.&lt;br/&gt;";
} else if ($result == __ERRNUM_INCORRECTPASSWORD)
{
        echo "Login failed. Incorrect password. Remember: Passwords are case sensitive.&lt;br/&gt;";
} else
{
        // should never get here
        echo "Serious error occurred!";
}</pre>
<p>Using a few cookies and a mysql INSERT query, you should be able to make a login and registration from here. I&#8217;m not going to give you all the code, that would be too easy!</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/login-via-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iPhone OS: UITableView</title>
		<link>http://ryanjd.com/index.php/iphone-os-uitableview/</link>
		<comments>http://ryanjd.com/index.php/iphone-os-uitableview/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 01:26:03 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[iPhone Development]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone OS]]></category>
		<category><![CDATA[UITableView]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=115</guid>
		<description><![CDATA[Many people don&#8217;t understand how to use a UITableView, and that&#8217;s okay. In truth, a UITableView is one of the easiest parts of the iPhone OS API to use &#8211; you simply have to know the names of the functions, and have a good understanding of Objective C &#8211; particularly delegation, as that is the [...]]]></description>
			<content:encoded><![CDATA[<p>Many people don&#8217;t understand how to use a UITableView, and that&#8217;s okay. In truth, a UITableView is one of the easiest parts of the iPhone OS API to use &#8211; you simply have to know the names of the functions, and have a good understanding of Objective C &#8211; particularly delegation, as that is the form of data input the UITableView uses. If you are unaware of what delegation is, you can look at <a href="http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocProtocols.html#//apple_ref/doc/uid/TP30001163-CH15-SW1">Apple&#8217;s Documentation</a> on delegation.</p>
<p>Creating a UITableView is easy. Mostly, UITableViews are inserted into UINavigationController&#8217;s, so we&#8217;ll do that too.</p>
<p><span id="more-115"></span></p>
<p>This is the initialisation code in AppDelegate.m:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
        UITableViewController <span style="color: #002200;">*</span>tableViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewController alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewStylePlain<span style="color: #002200;">&#93;</span>;
        navigationController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationController alloc<span style="color: #002200;">&#93;</span> initWithRootViewController<span style="color: #002200;">:</span>tableViewController<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>tableViewController release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>However, since the UITableView uses delegation as a datasource and for appearances, we need to subclass UITableViewController and change the required methods.</p>
<p>Once we&#8217;ve replaced all instances of &#8216;UITableViewController&#8217; with our custom controller (I have named mine DBTableViewController) then we can begin to alter the delegates methods. The required methods for a class that conforms to the UITableViewDataSource and the UITableViewDelegate protocols are as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView didSelectRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath;</pre></div></div>

<p>By responding to these methods, we can alter the appearance of the UITableView. For example, if we wanted to populate our custom UITableView class with cells labelled 1 through to 10 with 10 cells, we could use the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kNumberOfSections 2</span>
<span style="color: #6e371a;">#define kNumberOfRows 10</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Set up how many 'sections' (groups of rows)</span>
    <span style="color: #a61390;">return</span> kNumberOfSections;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Set up how many rows to have in our table view</span>
    <span style="color: #a61390;">return</span> kNumberOfRows;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CustomCell&quot;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">/*
     All this 'dequeueReusableCellWithIdentifier' stuff does is check whether
     an instance of the same cell (with the same identifier) has been deallocated yet.
     They are autoreleased, which means there may be one available to reuse, which would save memory.
     Memory is vital on the iPhone!
    */</span>
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleSubtitle reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    cell.textLabel.text		  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Row %d&quot;</span>, indexPath.row<span style="color: #002200;">&#93;</span>;
    cell.detailTextLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Section %d&quot;</span>, indexPath.section<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView didSelectRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Deselect this row, to conform the human interface guidelines</span>
    <span style="color: #002200;">&#91;</span>self.tableView deselectRowAtIndexPath<span style="color: #002200;">:</span>indexPath animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The result would look like this:</p>
<div id="attachment_132" class="wp-caption aligncenter" style="width: 283px"><a href="http://ryanjd.com/wp-content/uploads/2010/01/Screen-shot-2010-01-10-at-01.22.09.png"><img class="size-full wp-image-132  " title="UITableView Example" src="http://ryanjd.com/wp-content/uploads/2010/01/Screen-shot-2010-01-10-at-01.22.09.png" alt="Example" width="273" height="508" /></a><p class="wp-caption-text">Example of a list of cells using UITableView</p></div>
<p>However, if we wanted to build a UITableView from an array of items, it would be a slightly different story. For a start we would have to create a storage class for our data (an NSObject subclass) called (in our case) DBItem. DBItem looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DBItem <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
	UIImage <span style="color: #002200;">*</span>thumbnail;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>shortDescription;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIImage <span style="color: #002200;">*</span>thumbnail;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span>	  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, copy<span style="color: #002200;">&#41;</span>   <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>shortDescription;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> hasThumbnail;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// DBItem.m</span>
<span style="color: #6e371a;">#import &quot;DBItem.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> DBItem
&nbsp;
<span style="color: #a61390;">@synthesize</span> thumbnail;
<span style="color: #a61390;">@synthesize</span> title;
<span style="color: #a61390;">@synthesize</span> shortDescription;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>thumbnail release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>title release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>shortDescription release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Any initialisation</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> hasThumbnail <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>thumbnail<span style="color: #002200;">&#41;</span> ? <span style="color: #a61390;">YES</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>As you can see, all it is is a storage class. It simply contains a UIImage named &#8216;thumbnail&#8217;, has a name (an NSString*) that we called &#8216;title&#8217; and a &#8216;shortDescription&#8217; of the same type as the title. We could have just used an NSDictionary &#8211; however it is difficult to keep track of what is inside of the NSDictionary, whereas this class only ever contains these three properties. We are going to create a list of these items, and insert them into an NSMutableArray, which will be used by the UITableView as the source of its information.<br />
I have imported our new storage class into the current script using:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;DBItem.h&quot;</span></pre></div></div>

<p>Also, we need to set up the NSMutableArray in our custom UITableViewController subclass.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DBTableViewController <span style="color: #002200;">:</span> UITableViewController <span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>itemArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>itemArray;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Don&#8217;t forget to @synthesize itemArray in the .m file!</p>
<p>Now we can initialise and add a list of DBItem classes to the array.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define kNumberOfItems 6</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>itemArray release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithStyle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableViewStyle<span style="color: #002200;">&#41;</span>style <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithStyle<span style="color: #002200;">:</span>style<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Set the title of this controller</span>
		self.navigationItem.title <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;A List Of Items&quot;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Initialise itemArray</span>
		itemArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span>kNumberOfItems<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Insert a list of DBItem classes into itemArray</span>
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i <span style="color: #002200;">&amp;</span>lt; kNumberOfItems; i <span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>pathForImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;image%d.jpg&quot;</span>, i<span style="color: #002200;">&#93;</span>;
			<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title		   <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Thumbnail %d&quot;</span>, i<span style="color: #002200;">&#93;</span>;
			<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>description  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Description for item %d&quot;</span>, i<span style="color: #002200;">&#93;</span>;
			UIImage  <span style="color: #002200;">*</span>thumbnail    <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span>pathForImage<span style="color: #002200;">&#93;</span>;
			DBItem   <span style="color: #002200;">*</span>item		   <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>DBItem alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>item setTitle<span style="color: #002200;">:</span>title<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>item setShortDescription<span style="color: #002200;">:</span>description<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>item setThumbnail<span style="color: #002200;">:</span>thumbnail<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>itemArray addObject<span style="color: #002200;">:</span>item<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>item release<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>All we&#8217;re doing here is defining how many items we&#8217;ve got, and then setting up strings. Title will look like &#8216;Thumbnail 0&#8242; or 1, 2, 3, &#8230; etc, and description will look like &#8216;Description for item 0&#8242;, 1, 2, 3&#8230; etc. The images are named image0.jpg, image1.jpg, image2.jpg, image3.jpg until the end of the row count, which is kNumberOfItems. These items are then accessed by the array later on, as so:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Set up how many 'sections' (groups of rows)</span>
	<span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Set up how many rows to have in our table view</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>itemArray count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CustomCell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleSubtitle reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
	DBItem <span style="color: #002200;">*</span>item <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>itemArray objectAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	cell.imageView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item thumbnail<span style="color: #002200;">&#93;</span>;
	cell.textLabel.text  <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item title<span style="color: #002200;">&#93;</span>;
	cell.detailTextLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>item shortDescription<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>All we&#8217;re doing here is setting the contents of the UITableViewCell to the information stored inside our DBItem class. We could easily store our array of DBItem&#8217;s inside a singleton instance, known as a data manager. This is a unique program design that is easy to upkeep and is generally nice to get into the habit of doing. There will be an article here on singleton instances eventually.<br />
However, for now, we&#8217;ll finish UITableView&#8217;s. We have purposely left the data array as mutable so that we can edit it later. We&#8217;re going to do that now, by using the UITableView&#8217;s editing mode. This is activated as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
	self.navigationItem.rightBarButtonItem <span style="color: #002200;">=</span> self.editButtonItem;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The line &#8216;self.navigationItem.rightBarButtonItem = self.editButtonItem&#8217; adds a button to the right hand side of the navigation bar which contains the word &#8216;edit&#8217; or &#8216;done&#8217; depending on the UITableView&#8217;s editing mode. You&#8217;ve probably seen it before in, well, practically any app. This doesn&#8217;t complete editing yet! As with everything with UITableView&#8217;s, we need to set up the delegate methods:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView canEditRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// return NO if we don't want this specific cell to be edited.</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// this will make the first cell uneditable.</span>
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
	<span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// every other cell is editable!</span>
		<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView commitEditingStyle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableViewCellEditingStyle<span style="color: #002200;">&#41;</span>editingStyle forRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>editingStyle <span style="color: #002200;">==</span> UITableViewCellEditingStyleDelete<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// this means the user has committed a 'delete action'</span>
		<span style="color: #11740a; font-style: italic;">// something such as swiping it then pressing 'delete' or hitting edit</span>
		<span style="color: #11740a; font-style: italic;">// then tapping to select that row, and pushing delete</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Need to remove it from the array</span>
		<span style="color: #002200;">&#91;</span>itemArray removeObjectAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Animate a deletion of it from the UITableView</span>
        <span style="color: #002200;">&#91;</span>tableView deleteRowsAtIndexPaths<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObject<span style="color: #002200;">:</span>indexPath<span style="color: #002200;">&#93;</span> withRowAnimation<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>editingStyle <span style="color: #002200;">==</span> UITableViewCellEditingStyleInsert<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// This is when the user has chosen to add a cell to the UITableView</span>
		<span style="color: #11740a; font-style: italic;">// However, we don't need this now!</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView moveRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>fromIndexPath toIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>toIndexPath <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// This method is just telling us that the user has rearranged the views</span>
	<span style="color: #11740a; font-style: italic;">// They have moved row 'fromIndexPath' to row 'toIndexPath'</span>
	<span style="color: #11740a; font-style: italic;">// So we need to change our data accordingly</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>itemArray exchangeObjectAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>fromIndexPath row<span style="color: #002200;">&#93;</span> withObjectAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>toIndexPath row<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView canMoveRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// This is asking whether a certain cell should be able to be reordered or not.</span>
	<span style="color: #11740a; font-style: italic;">// We'll disable row 0 but enable all else</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>indexPath row<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>; <span style="color: #a61390;">else</span> <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>After this, you should have a result much like this (except with different images):</p>
<div id="attachment_144" class="wp-caption aligncenter" style="width: 424px"><a href="http://ryanjd.com/wp-content/uploads/2010/01/Screen-shot-2010-01-10-at-21.04.55.png"><img class="size-full wp-image-144" title="Screen shot 2010-01-10 at 21.04.55" src="http://ryanjd.com/wp-content/uploads/2010/01/Screen-shot-2010-01-10-at-21.04.55.png" alt="" width="414" height="770" /></a><p class="wp-caption-text">iPhone simulator showing an editable UITableView with DBItem cells</p></div>
<p>And that&#8217;s pretty much all there is to know! To know how to save the final edited array, so that it can be used the next time the user re-opens the app, look into NSCoding and NSData saving to a file. I&#8217;ll add to this article at some point with more information about this! But until then, enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/iphone-os-uitableview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Verlet Physics</title>
		<link>http://ryanjd.com/index.php/verlet-physics/</link>
		<comments>http://ryanjd.com/index.php/verlet-physics/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 11:36:19 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[Game Tutorials]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[designing]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[making]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ragdoll]]></category>
		<category><![CDATA[verlet]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=86</guid>
		<description><![CDATA[Ever wondered how games have that perfect movement effect where the objects bump into each other and flow perfectly? There is a lot to this effect, and today we&#8217;re going to learn about the basis of it: Verlet physics. If you&#8217;ve ever wandered into the realms of game programming, you&#8217;ll probably have used a movement [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wondered how games have that perfect movement effect where the objects bump into each other and flow perfectly? There is a lot to this effect, and today we&#8217;re going to learn about the basis of it: Verlet physics.</p>
<p>If you&#8217;ve ever wandered into the realms of game programming, you&#8217;ll probably have used a movement code much like this pseudocode:<br />
<code> </code></p>
<pre>position = position + velocity</pre>
<p>So, in real code, it looks like this:<br />
<code> </code></p>
<pre>x += x_velocity;
y += y_velocity;</pre>
<p>This type of movement is known as <a href="http://en.wikipedia.org/wiki/Leonhard_Euler">Euler</a> movement. In this tutorial we&#8217;re going to learn about a new type of movement: <a href="http://en.wikipedia.org/wiki/Verlet_integration">Verlet</a>.</p>
<p>Verlet works like this:</p>
<pre>position = (position * 2) - position + acceleration</pre>
<p>To see the difference, look at this example: the ball on the left uses verlet movement, and the one on the right uses euler movement.<span id="more-86"></span></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://ryanjd.com/wp-content/uploads/2009/08/tut1.swf" /><embed type="application/x-shockwave-flash" width="550" height="400" src="http://ryanjd.com/wp-content/uploads/2009/08/tut1.swf"></embed></object></p>
<p>The code for movement is here: (&#8216;ball1&#8242; is the ball on the left, and &#8216;ball2&#8242; is the ball on the right)</p>
<pre>var sy;
sy = ball1._y;
ball1._y = (ball1._y * 2) - ball1.yp + gravity;
ball1.yp = sy;
ball2._y += gravity;</pre>
<p>Gravity is predefined as &#8217;0.1&#8242;.</p>
<p>As you can see, the y position of ball1 is multiplied by 2 and the previous y position is taken away. This creates the difference between the current position and the previous position &#8211; so, by simply changing the position, the movement engine generates acceleration. So, if you were to set the position of a ball to (100, 100) as (x, y); then if you set the position to (101, 100) without setting the previous position &#8211; the offset is 1 on the x axis and will be used as velocity. The ball will move 1 pixel to the right each step if you keep doing this. This is essentially what the acceleration does in the formula.</p>
<p>By using verlet physics, you can create ragdoll physics far more easily.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/verlet-physics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Introduction to PHP Classes</title>
		<link>http://ryanjd.com/index.php/intro-php-classes/</link>
		<comments>http://ryanjd.com/index.php/intro-php-classes/#comments</comments>
		<pubDate>Sat, 08 Aug 2009 00:13:47 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[objects]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=51</guid>
		<description><![CDATA[About Object-Oriented Programming Many programming languages are object-oriented. However, the method of defining an object varies from language to language. Some examples of object-oriented languages are: Java JavaScript C++ and finally, PHP Basic Class Programming in PHP In PHP, an object is defined as this: class myObject { //variables and methods go here } Methods [...]]]></description>
			<content:encoded><![CDATA[<h2>About Object-Oriented Programming</h2>
<p>Many programming languages are object-oriented. However, the method of defining an object varies from language to language. Some examples of object-oriented languages are:</p>
<ul>
<li>Java</li>
<li>JavaScript</li>
<li>C++</li>
<li>and finally, PHP</li>
</ul>
<h2>Basic Class Programming in PHP</h2>
<p>In PHP, an object is defined as this:<br />
<span id="more-51"></span><br />
<code> </code></p>
<pre>class myObject {
    //variables and methods go here
}</pre>
<p>Methods are easy to add to classes. If you wanted to add a function to myObject that returns the current server time, you could do this:<br />
<code> </code></p>
<pre>class myObject{
	function getServerTime() {
		return time();
	}
}</pre>
<p>Easy as that. Defining variables is a little bit trickier. There are many ways to define variables, but we&#8217;ll keep it simple for now and use the &#8216;var&#8217; method.<br />
<code> </code></p>
<pre>class myObject {
        //this is a valid assignment
	var $variable     = 'constant value';

        //this is invalid because it is assigning to a function. This can't be done.
	var $serverTime = time();
}</pre>
<p>To access internal variables, we have to use the <strong>$this</strong> variable. <strong>$this</strong> is a pointer that points internally, so that we can access the variables we just declared. So, we&#8217;re going to add a function to &#8216;myObject&#8217; and access $variable.<br />
<code> </code></p>
<pre>class myObject {
        //this is a valid assignment
	var $variable     = 'Hello World!';
	function GetVariable() {
		//notice how there is no dollar sign $ in front of the variable
		return $this-&gt;variable; //this will return 'Hello World!'
	}
}</pre>
<p>Notice how there is no dollar sign ($) in front of the variable we are pointing to with <strong>$this</strong>. This is also how you call methods internally.</p>
<p>On the last note of basic PHP classes, you will need to know how to create an instance of your class. Once you have created an instance of your class, you can use it in the same way you used &#8216;this&#8217;. To create an instance, use the method:<br />
<code> </code></p>
<pre>$instance = new ClassName();</pre>
<p>This creates a new instance of the class &#8216;ClassName&#8217;. So, an example using everything we&#8217;ve learned:<br />
<code> </code></p>
<pre>class myClass {
	var $string = 'Hello World!';
	function ShowString() {
		echo $this-&gt;string;
	}
}
$instance = new myClass();
$instance-&gt;ShowString();</pre>
<p>Also, it is entirely possible to create an instance of a class from a string:<br />
<code
<pre>
class MyClass {
	function Exist() {
		echo "My class name is ".get_class($this)."";
	}
}

$classname = 'MyClass';
$instance = new $classname();
$instance->Exist();
//this will echo 'My class name is MyClass'
</code>

That&#8217;s about it on a basic level! Now for some more advanced usage of classes.
<h2>Advanced Classes</h2>

Now that you know how to use variables, it&#8217;s time to learn about private and public variables. Basically, a private variable can only be accessed internally by the class itself; and a public variable can be accessed by everything. A variable you would want to keep private could be the password of a user class, for example. A public variable could be the title of the page. Public and private variables are very useful.

Here is a class that uses public and private variables:

<code> </code>
<pre>class myClass {
	private $private_var = "This variable is private! You can't see this!";
	public  $public_var  = "This variable is public! Anyone can see this!";

	//note- you can also use public and private modifiers on functions
	//this is useful because internal functions should be private
	//and external functions could use more than one private function
	//it's good to make a network of public and private methods and variables
	public function PrintPrivate() {
		var_dump($this-&gt;private_var);
	}
}
$instance = new myClass();

//this line will print the contents of $private_var to the screen
//because it is being called internally using an external function of myClass
$instance-&gt;PrintPrivate();

//however, this line will print an error - private variables can't be accessed
//this is because it is being accessed externally
var_dump($instance-&gt;private_var);</pre>
<p>When an instance of a class is created, the method (if it exists) $instance-&gt;__construct(arguments). So, with this knowledge, you should have realised that when calling:<code>$instance = new myClass(arguments)</code> the arguments are sent to the method __contruct inside myClass. This method is called a <strong>constructor</strong>. When an instance is deleted, the class <strong>destructor</strong> is called. The destructor is called when either every variable inside the class has been removed using the PHP function <strong>unset</strong>($variable) or at the end of the script.<br />
<code> </code></p>
<pre>class myClass {
	public $id;
	public function __construct($id) {
		$this-&gt;id = $id;
		echo "Constructed {$this-&gt;id}
";
	}
	public function __destruct() {
		echo "Destructed {$this-&gt;id}
";
	}
}

$count = 0;
$instance = Array();

//this line calls the 'construct' method and creates a new instance
$instance[$count] = new myClass($count);
$count ++;

$instance[$count] = new myClass($count); $count ++;
$instance[$count] = new myClass($count); $count ++;</pre>
<p>This code will echo:<br />
<code> </code></p>
<pre>Constructed 0
Constructed 1
Constructed 2
Destructed 0
Destructed 1
Destructed 2</pre>
<p>You can see that the constructor is called when the instance is created, and that the destructor is called when the script ends.</p>
<p>Now that we understand all of that, we can move on to&#8230;</p>
<h2>Static Classes</h2>
<p>So far, the classes have held variables, and have been individual instances. However, if, for example, you wanted a variable that had a set value and applied to all instances of the class, how would you do it? The answer is static variables:<br />
<code> </code></p>
<pre>class myClass {

 //the use of 'static' can be before or after 'public'
 //and it makes the variable global across the class
 static public $variable = 'Hello World!&lt;br/&gt;';

 static public function ShowText() {
 //for static variables, we use self:: instead of $this-&gt;
 echo self::$variable;
 }

 //notice that there is no constructor

}

//there is no need to construct a static class!
myClass::ShowText();
//this will output 'Hello World!'

//however, it is possible to create instances and use static at the same time
$class = Array();
$class[0] = new myClass();

//this is an invalid static function caller:
//$class[0]::ShowText();
//this will result in a parse error!

//this will work:
$class[0]-&gt;ShowText();
//it works because the function can be accessed
//statically and non-statically

$class[1] = new myClass();

//let's try something!
var_dump($class[0]-&gt;variable); echo "&lt;br/&gt;";
var_dump($class[1]-&gt;variable); echo "&lt;br/&gt;";
//these will both echo NULL because using -&gt;
//as a pointer is incorrect!

$class[0]-&gt;variable = 'I\'m class one!';
$class[1]-&gt;variable = 'I\'m class two!';

$class[0]-&gt;ShowText();
$class[1]-&gt;ShowText();
//these will both echo 'Hello World!' because
//using -&gt; as an operator will only change
//the class instance value of $variable
//which previously didn't exist.

//let's try changing it statically and echoing again!
myClass::$variable = 'This text has been changed!&lt;br/&gt;';
$class[0]-&gt;ShowText();
$class[1]-&gt;ShowText();
//these will both echo 'This text has been changed!' because
//the variable has statically been changed
//and this applies to all instances of the class</pre>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/intro-php-classes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Repairing MySql Databases with PHP &#8211; Troubleshooting and Code</title>
		<link>http://ryanjd.com/index.php/repairing-mysql-databases-with-php-troubleshooting-and-code/</link>
		<comments>http://ryanjd.com/index.php/repairing-mysql-databases-with-php-troubleshooting-and-code/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 01:56:00 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[PHP Examples]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[repair]]></category>
		<category><![CDATA[troubleshoot]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=26</guid>
		<description><![CDATA[I&#8217;ve been playing around with the code from the previous tutorial (Is it possible to repair mysql databases with php?) and came up with a function that repairs a mysql database. I&#8217;m working on storing the database in an XML document, and importing it into PHP. Here&#8217;s the code for the function &#8216;Troubleshoot()&#8217; inside Database: [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with the code from the previous tutorial (<a href="http://ryanjd.com/index.php/is-it-possible-to-repair-mysql-databases-with-php/">Is it possible to repair mysql databases with php?</a>) and came up with a function that repairs a mysql database. I&#8217;m working on storing the database in an XML document, and importing it into PHP.</p>
<p>Here&#8217;s the code for the function &#8216;Troubleshoot()&#8217; inside Database:<br />
<span id="more-26"></span><br />
<code>
<pre>
static public function Troubleshoot() {
		//make a list of all tables in the database
		$TABLES = Array();
		$table_list = self::Query("show tables");
		while($row = self::Fetch($table_list)) {
			array_push( $TABLES, $row['Tables_in_database'] );
		}

		//check for tables that don't exist in the mysql database and create
		foreach(self::$STRUCTURE as $key => $index) {
			if( !in_array( $key, $TABLES ) ) {
				echo "'{$key}' doesn't exist! Creating... ";
				if($create_query = Database::GetCreateQuery($key)) {
					Database::Query($create_query);
					echo "Table created!";
				} else {
					echo "Failed to create table.";
				}
			}
		}
		echo "<br/>";
		//repair currently existing tables
		foreach($TABLES as $name) {
			if( array_key_exists($name, self::$STRUCTURE) ) {
				echo "Troubleshooting table '{$name}' : {<br/>";
				$table_structure_query = self::Query( "describe {$name}" );
				$i = 0; while($table_structure = self::Fetch($table_structure_query)) {
					$table_row = self::$STRUCTURE[$name][$i];

					//Convert some of the table schemas to the easier to use ones stored in self::$STRUCTURE
					$table_structure['Null'] = ( ($table_structure['Null'] == 'NO') ? 'not null' : 'null' );
					$table_structure['Key']  = ( ($table_structure['Key'] == 'PRI') ? "PRIMARY KEY({$table_structure['Field']})" : '' );

					//check the table schema matches
					if(
					$table_structure['Field'] == $table_row['field'] &#038;&#038;
					$table_structure['Type']  == $table_row['type'] &#038;&#038;
					$table_structure['Null']  == $table_row['null'] &#038;&#038;
					$table_structure['Key']   == $table_row['primary key'] &#038;&#038;
					$table_structure['Extra'] == $table_row['extra']
					) {
						echo "
<p style='text-indent: 32px;'>The table '{$table_structure['Field']}' is intact and built properly.

";
					} else {
						echo "
<p style='text-indent: 32px;'>The row '{$table_structure['Field']}' is corrupted. Attempting to repair...";
						if(self::RepairTable($name)) echo "Repaired!";
						echo "

";
					}

					$i ++;
				}
				echo "}<br/><br/>";
			}
		}
	}</pre>
<p></code><br />
I&#8217;ll edit this all another day to make it more comprehensible. Right now, though, it&#8217;s 3am and I&#8217;m too tired to concentrate.</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/repairing-mysql-databases-with-php-troubleshooting-and-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is it possible to repair MySql Databases with PHP?</title>
		<link>http://ryanjd.com/index.php/is-it-possible-to-repair-mysql-databases-with-php/</link>
		<comments>http://ryanjd.com/index.php/is-it-possible-to-repair-mysql-databases-with-php/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 13:08:21 +0000</pubDate>
		<dc:creator>Rokan</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[repair]]></category>
		<category><![CDATA[troubleshoot]]></category>

		<guid isPermaLink="false">http://ryanjd.com/?p=6</guid>
		<description><![CDATA[I am attempting to devise a method of storing the structure of a MySql database and it&#8217;s individual tables in PHP. So far, it looks a bit like this: public static $STRUCTURE = Array( 'members' => Array( Array( 'field' => 'id', 'type' => 'int(11)', 'null' => 'not null', 'extra' => 'auto_increment', 'primary key' => ', [...]]]></description>
			<content:encoded><![CDATA[<p>I am attempting to devise a method of storing the structure of a MySql database and it&#8217;s individual tables in PHP. So far, it looks a bit like this:</p>
<p><code>
<pre>public static $STRUCTURE = Array(
	'members' => Array(
		Array(
			'field' => 'id',
			'type' => 'int(11)',
			'null' => 'not null',
			'extra' => 'auto_increment',
			'primary key' => ', PRIMARY KEY(id)',
			'suffix' => ','
		),
		Array(
			'field' => 'member_name',
			'type' => 'varchar(32)',
			'null' => 'not null',
			'suffix' => ','
		),
		Array(
			'field' => 'member_pass_hash',
			'type' => 'varchar(32)',
			'null' => 'not null'
		)
	)
);</pre>
<p></code><br />
<span id="more-6"></span><br />
This may look complicated and overkill &#8211; however, this could easily be loaded from an ini file, another php file, and updated from the mysql database itself (with the latter being the easiest option). However, updating the structure from the database can obviously only be done when the database is functioning perfectly. This way, the array self::$STRUCTURE can be used to repair the database and to create queries. For example, the query:<br />
<code>CREATE TABLE members( id INT NOT NULL AUTO_INCREMENT , PRIMARY KEY(id) );</code></p>
<p>This query could easily be generated by using the following structure:</p>
<p><code>
<pre>private static $STRUCTURE = Array(
	'members' => Array(
		Array(
			'field' => 'id',
			'type' => 'int(11)',
			'null' => 'not null',
			'extra' => 'auto_increment',
			'primary key' => ', PRIMARY KEY(id)'
		)
	)
);</pre>
<p></code></p>
<p>A function could be devised to transform this table structure into a mysql query. Something like this:<br />
<code>
<pre>static public function GetCreateQuery($table) {
	$structure = self::GetTableStructure($table);
	if($structure != 0) {
		$query = "CREATE TABLE " . $table . "( ";
		foreach($structure as $index) {
			$query .= implode(' ', $index) . " ";
		}
		$query .= ");";
		return $query;
	} else {
		return false;
	}
}</pre>
<p></code></p>
<p>This function should return a valid MySql query if set up properly. If you run the query in your MySql panel, it creates the correct table with primary key and extra values. It should be easy from here to design a system that can troubleshoot its own MySql tables. For example, to repair a table, a query like this could be used (but obviously altered):<br />
<code>CREATE TABLE new_members_table(id int(11) not null auto_increment);<br />
INSERT INTO new_members_table SELECT * FROM old_members_table;<br />
DROP TABLE old_members_table;<br />
RENAME TABLE new_members_table TO old_members_table;</code></p>
<p>Notice that the structure of the table created is the same as was created by our &#8216;GetCreateQuery&#8217; function, and was structured by the self::$STRUCTURE['members'] array.</p>
<p>Using this could improve your PHP blogging system, forum software, commenting software.. any kind of web software which uses PHP and MySql databases &#8211; so there is no limit to its use!</p>
]]></content:encoded>
			<wfw:commentRss>http://ryanjd.com/index.php/is-it-possible-to-repair-mysql-databases-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
