<?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>D17's blog &#187; UITextField</title>
	<atom:link href="http://blog.d-17.com/tag/uitextfield/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.d-17.com</link>
	<description>About hardcore technology and softcore business</description>
	<lastBuildDate>Thu, 22 Apr 2010 09:18:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Combining multiple UITextFields and a UITableView in a nice way for an iPhone app</title>
		<link>http://blog.d-17.com/2009/08/combining-multiple-uitextfields-and-a-uitableview-in-a-nice-way-for-an-iphone-app/</link>
		<comments>http://blog.d-17.com/2009/08/combining-multiple-uitextfields-and-a-uitableview-in-a-nice-way-for-an-iphone-app/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 23:54:05 +0000</pubDate>
		<dc:creator>Dirk de Kok</dc:creator>
				<category><![CDATA[iphone]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITextField]]></category>

		<guid isPermaLink="false">http://blog.d-17.com/?p=85</guid>
		<description><![CDATA[Update: also read Part 2 explaining a different way of solving this problem.
So a decent number of iPhone apps will not only show things like data, but also offer you the possibility to edit data. Many controls like UISlider, UISwitch and UITextfield are provided by Apple to create some type of edit screen, almost like [...]]]></description>
			<content:encoded><![CDATA[<p>Update: also read <a href="/2009/10/combining-multiple-uitextfields-and-a-uitableview-in-a-nice-way-for-an-iphone-app-part-2/">Part 2</a> explaining a different way of solving this problem.</p>
<p>So a decent number of iPhone apps will not only show things like data, but also offer you the possibility to edit data. Many controls like <a href="http://developer.apple.com/iphone/library/documentation/uikit/reference/UISlider_Class/Reference/Reference.html">UISlider</a>, <a href="http://developer.apple.com/iphone/library/documentation/uikit/reference/UISwitch_Class/Reference/Reference.html">UISwitch</a> and <a href="http://developer.apple.com/iphone/library/documentation/uikit/reference/UITextField_Class/Reference/UITextField.html">UITextfield</a> are provided by Apple to create some type of edit screen, almost like a web form on a HTML page. Creating a simple form that only covers the top half of your screen is simple: you create your elements like UITextField at the top, and when you touch inside the field the onscreen keyboard will show up. You hit a Done Button or a non-UITextField control and the keyboard disappears again.</p>
<p>Now, when you have more data to add/edit than will fit in half a screen, things get more daunting:</p>
<ol>
<li>the keyboard should only be visible when editing a UITextField</li>
<li>when touching a UITextField so that the keyboard becomes visible, it should not cover the field you are editing</li>
<li>with the keyboard visible you should still be able to scroll to all elements of your editing form, and not have parts be covered indefinitely by the keyboard</li>
</ol>
<p>Well, the most natural kind of view to use to display all controls is a UITableView. Apple uses it herself, look at the editing screen for your Mail.app settings. But when you just throw in a UITableView, you run into problems:</p>
<ol>
<li>when scrolling off screen, table cells are reused and therefore also its subviews like UITextFields, which causes your newly entered data to be destroyed</li>
<li>when having a UITextField being firstResponder (cursor in it),  you scroll it off screen and the touch inside another UITextField that then becomes firstResponder, your app will crash. This is problably because the first UITextField is destroyed before it can resignFirstResponder</li>
</ol>
<p>So how do we do this then? Apple does use a UITableView in some way, but the above problems are show stoppers. Well the answer is: use a UITableView, but don&#8217;t let it scroll.</p>
<p><span id="more-85"></span><br />
The trick is to nest a UITableView inside a UIScrollView. And then to give the table and the contentSize of exactly the same size that the whole form is. So if you have 10 cells each 44 pixels high, you create a UITableView with a height of 440, and put that inside a UIScrollView with a contentSize that is also 440 pixels high. This way the table is exactly the size of all the cells combined. This way no scrolling for the UITableView and therefore no reuse of cell&#8217;s with all it&#8217;s problems.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-90" style="margin-top: 25px; margin-bottom: 25px;" title="nesting_uitableview_inside_uiscrollview" src="http://blog.d-17.com/wp-content/uploads/2009/08/nesting_uitableview_inside_uiscrollview.png" alt="nesting_uitableview_inside_uiscrollview" width="830" height="394" /></p>
<p>Here&#8217;s the code to set the size correctly:</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400;">// actually more object in reality</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #c41a16;"><span style="color: #aa0d91;">self</span><span style="color: #000000;">.</span><span style="color: #3f6e74;">countries</span><span style="color: #000000;"> = [[</span><span style="color: #5c2699;">NSMutableArray</span><span style="color: #000000;"> </span><span style="color: #2e0d6e;">alloc</span><span style="color: #000000;">]</span><span style="color: #2e0d6e;">initWithObjects</span><span style="color: #000000;">: </span>@&#8221;Belgium&#8221;<span style="color: #000000;">, </span>@&#8221;Brittain&#8221;<span style="color: #000000;">,nil];</span></p>
<p><span style="font-family: Monaco, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: 10px; color: #3f6e74;"><span style="color: #000000;"><span style="white-space: pre;"><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; white-space: normal; font-size: 13px; line-height: 19px;"> </span> </span>[</span><span style="color: #aa0d91;">self</span><span style="color: #000000;">.</span>scrollView<span style="color: #000000;"> </span><span style="color: #2e0d6e;">setContentSize</span><span style="color: #000000;">:</span><span style="color: #2e0d6e;">CGSizeMake</span><span style="color: #000000;">(</span><span style="color: #1c00cf;">320</span><span style="color: #000000;">, (</span>CELL_HEIGHT<span style="color: #000000;"> * [</span><span style="color: #aa0d91;">self</span><span style="color: #000000;">.</span>countries<span style="color: #000000;"> </span><span style="color: #2e0d6e;">count</span><span style="color: #000000;">]))];</span></span></p>
<p><span style="font-family: Monaco, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: normal; font-size: 10px; color: #3f6e74;"><span style="color: #000000;"><span style="color: #3f6e74;"><span style="color: #000000;"><span style="white-space: pre;"> </span>[</span><span style="color: #aa0d91;">self</span><span style="color: #000000;">.</span>tableView<span style="color: #000000;"> </span><span style="color: #2e0d6e;">setFrame</span><span style="color: #000000;">:</span><span style="color: #2e0d6e;">CGRectMake</span><span style="color: #000000;">(</span><span style="color: #1c00cf;">0</span><span style="color: #000000;">, </span><span style="color: #1c00cf;">0</span><span style="color: #000000;">, </span><span style="color: #1c00cf;">320</span><span style="color: #000000;">, (</span>CELL_HEIGHT<span style="color: #000000;"> * [</span><span style="color: #aa0d91;">self</span><span style="color: #000000;">.</span>countries<span style="color: #000000;"> </span><span style="color: #2e0d6e;">count</span><span style="color: #000000;">]))];</span></span></span></span></p>
<p>Well, that&#8217;s the main trick. Added to that, you want to resize the frame when the onscreen keyboard is shown and scroll the UITextField you touched in to scroll into view. Have a look at the source code to see how that&#8217;s done.</p>
<p><a href="http://blog.d-17.com/wp-content/uploads/2009/08/TextfieldsWithKeyboard.zip">Download TextfieldsWithKeyboard XCode iPhone project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.d-17.com/2009/08/combining-multiple-uitextfields-and-a-uitableview-in-a-nice-way-for-an-iphone-app/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
