<?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>Webd &#187; Cocoa Touch</title>
	<atom:link href="http://webd.fr/tag/cocoa-touch/feed" rel="self" type="application/rss+xml" />
	<link>http://webd.fr</link>
	<description>Le blog de Julien Quéré</description>
	<lastBuildDate>Tue, 22 Mar 2011 15:49:19 +0000</lastBuildDate>
	<language>fr</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Nib2objc: Ou comment convertir un nib en code Objectif-C</title>
		<link>http://webd.fr/735-nib2objc-ou-comment-convertir-un-nib-en-code-objectif-c</link>
		<comments>http://webd.fr/735-nib2objc-ou-comment-convertir-un-nib-en-code-objectif-c#comments</comments>
		<pubDate>Wed, 10 Jun 2009 14:49:40 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=735</guid>
		<description><![CDATA[L&#8217;utilisation d&#8217;Interface Builder s&#8217;avère très pratique pour les développeurs Cocoa Touch. C&#8217;est vrais que c&#8217;est génial de pouvoir créer ses interfaces en mode WYSIWYG. Oui mais voilà, cela peut poser quelques problèmes. Outre les cas où l&#8217;utilisation du code est &#8230; <a href="http://webd.fr/735-nib2objc-ou-comment-convertir-un-nib-en-code-objectif-c">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>L&#8217;utilisation d&#8217;Interface Builder s&#8217;avère très pratique pour les développeurs Cocoa Touch. C&#8217;est vrais que c&#8217;est génial de pouvoir créer ses interfaces en mode WYSIWYG. Oui mais voilà, cela peut poser quelques problèmes. Outre les cas où l&#8217;utilisation du code est nécessaire (pour atteindre la granularité voulue), on peut vouloir renoncer à l&#8217;utilisation des nibs pour des raisons de performance. </p>
<p>En effet, dans certains cas l&#8217;utilisation des nibs pose de sérieux problèmes de performance. On se retrouve ainsi avec un dur choix: soit on choisit la simplicité (et on utilise Interface Builder), soit on écrit ses interfaces directement dans le code et on gagne en performance. Dans certains cas, l&#8217;impact de l&#8217;utilisation des nibs sur la performance est quasi nul. La question ne se pose donc pas. Mais parfois, ce n&#8217;est pas le cas.<br />
<span id="more-735"></span><br />
Dans ce dernier cas de figure, il existe maintenant une solution: nib2objc. Ce projet Open-Source  est tout simplement un convertisseur de fichiers nib (.xib) vers du code Objectif-C. Il gère toutes les propriétés publiques de chacun des éléments graphiques, le constructeur et la hiérarchie des vues. Pour le moment, il n&#8217;y a que les composants d&#8217;UIKit qui sont supportés. L&#8217;utilisation est on-ne-peut-plus simple:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p735code3'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7353"><td class="code" id="p735code3"><pre class="text" style="font-family:monospace;">imac-de-julien-2:Debug julien$ ./nib2objc demo.xib &gt; demo.m</pre></td></tr></table></div>

<p><a href="http://webd.fr/wp-content/uploads/2009/06/image-6.png" rel="lightbox[735]" title="image-6"><img src="http://webd.fr/wp-content/uploads/2009/06/image-6-300x224.png" alt="image-6" title="image-6" width="300" height="224" class="aligncenter size-medium wp-image-738" /></a></p>
<p>Et voilà le résultat pour ce nib:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p735code4'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7354"><td class="code" id="p735code4"><pre class="objc" style="font-family:monospace;">UILabel <span style="color: #002200;">*</span>view6 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">20.0</span>, <span style="color: #2400d9;">20.0</span>, <span style="color: #2400d9;">260.0</span>, <span style="color: #2400d9;">21.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
view6.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">20.0</span>, <span style="color: #2400d9;">20.0</span>, <span style="color: #2400d9;">260.0</span>, <span style="color: #2400d9;">21.0</span><span style="color: #002200;">&#41;</span>;
view6.adjustsFontSizeToFitWidth <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view6.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.000</span>;
view6.autoresizingMask <span style="color: #002200;">=</span> UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
view6.baselineAdjustment <span style="color: #002200;">=</span> UIBaselineAdjustmentAlignCenters;
view6.clearsContextBeforeDrawing <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view6.clipsToBounds <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view6.contentMode <span style="color: #002200;">=</span> UIViewContentModeScaleToFill;
view6.enabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view6.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Helvetica&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">17.000</span><span style="color: #002200;">&#93;</span>;
view6.hidden <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view6.lineBreakMode <span style="color: #002200;">=</span> UILineBreakModeTailTruncation;
view6.minimumFontSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">10.000</span>;
view6.multipleTouchEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view6.numberOfLines <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
view6.opaque <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view6.shadowOffset <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;
view6.tag <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
view6.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ma célule personalisée&quot;</span>;
view6.textAlignment <span style="color: #002200;">=</span> UITextAlignmentCenter;
view6.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span><span style="color: #2400d9;">0.000</span> green<span style="color: #002200;">:</span><span style="color: #2400d9;">0.000</span> blue<span style="color: #002200;">:</span><span style="color: #2400d9;">0.000</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span><span style="color: #002200;">&#93;</span>;
view6.userInteractionEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
UITableViewCell <span style="color: #002200;">*</span>view4 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">300.0</span>, <span style="color: #2400d9;">182.0</span><span style="color: #002200;">&#41;</span> reuseIdentifier<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>null<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
view4.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">0.0</span>, <span style="color: #2400d9;">300.0</span>, <span style="color: #2400d9;">182.0</span><span style="color: #002200;">&#41;</span>;
view4.accessoryType <span style="color: #002200;">=</span> UITableViewCellAccessoryNone;
view4.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.000</span>;
view4.autoresizingMask <span style="color: #002200;">=</span> UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
view4.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithRed<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span> green<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span> blue<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span><span style="color: #002200;">&#93;</span>;
view4.clearsContextBeforeDrawing <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view4.clipsToBounds <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view4.contentMode <span style="color: #002200;">=</span> UIViewContentModeScaleToFill;
view4.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Helvetica&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">17.000</span><span style="color: #002200;">&#93;</span>;
view4.hidden <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view4.hidesAccessoryWhenEditing <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view4.indentationLevel <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
view4.indentationWidth <span style="color: #002200;">=</span> <span style="color: #2400d9;">10.000</span>;
view4.lineBreakMode <span style="color: #002200;">=</span> UILineBreakModeTailTruncation;
view4.multipleTouchEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view4.opaque <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view4.selectedTextColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithWhite<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span><span style="color: #002200;">&#93;</span>;
view4.selectionStyle <span style="color: #002200;">=</span> UITableViewCellSelectionStyleBlue;
view4.showsReorderControl <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view4.tag <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
view4.textAlignment <span style="color: #002200;">=</span> UITextAlignmentLeft;
view4.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor colorWithWhite<span style="color: #002200;">:</span><span style="color: #2400d9;">0.000</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span><span style="color: #002200;">&#93;</span>;
view4.userInteractionEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
&nbsp;
UIButton <span style="color: #002200;">*</span>view7 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeRoundedRect<span style="color: #002200;">&#93;</span>;
view7.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">101.0</span>, <span style="color: #2400d9;">49.0</span>, <span style="color: #2400d9;">99.0</span>, <span style="color: #2400d9;">37.0</span><span style="color: #002200;">&#41;</span>;
view7.adjustsImageWhenDisabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view7.adjustsImageWhenHighlighted <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view7.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.000</span>;
view7.autoresizingMask <span style="color: #002200;">=</span> UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
view7.clearsContextBeforeDrawing <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.clipsToBounds <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.contentHorizontalAlignment <span style="color: #002200;">=</span> UIControlContentHorizontalAlignmentCenter;
view7.contentMode <span style="color: #002200;">=</span> UIViewContentModeScaleToFill;
view7.contentVerticalAlignment <span style="color: #002200;">=</span> UIControlContentVerticalAlignmentCenter;
view7.enabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view7.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Helvetica-Bold&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">15.000</span><span style="color: #002200;">&#93;</span>;
view7.hidden <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.highlighted <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.multipleTouchEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.opaque <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.reversesTitleShadowWhenHighlighted <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.selected <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.showsTouchWhenHighlighted <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view7.tag <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
view7.titleShadowOffset <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">1.0</span>, <span style="color: #2400d9;">1.0</span><span style="color: #002200;">&#41;</span>;
view7.userInteractionEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#91;</span>view7 setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Un bouton&quot;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view7 setTitleColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor colorWithWhite<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span> alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1.000</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateHighlighted<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view7 setTitleColor<span style="color: #002200;">:</span>NSPatternColorSpace <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImage_Class/"><span style="color: #400080;">NSImage</span></a> 0x2c3e5a0 Size<span style="color: #002200;">=</span><span style="color: #002200;">&#123;</span><span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">460</span><span style="color: #002200;">&#125;</span> Reps<span style="color: #002200;">=</span><span style="color: #002200;">&#40;</span>
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/"><span style="color: #400080;">NSBitmapImageRep</span></a> 0x2c49460 Size<span style="color: #002200;">=</span><span style="color: #002200;">&#123;</span><span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">460</span><span style="color: #002200;">&#125;</span> ColorSpace<span style="color: #002200;">=</span>NSDeviceRGBColorSpace BPS<span style="color: #002200;">=</span><span style="color: #2400d9;">8</span> BPP<span style="color: #002200;">=</span><span style="color: #2400d9;">32</span> Pixels<span style="color: #002200;">=</span>320x460 Alpha<span style="color: #002200;">=</span><span style="color: #a61390;">YES</span> Planar<span style="color: #002200;">=</span><span style="color: #a61390;">NO</span> Format<span style="color: #002200;">=</span><span style="color: #2400d9;">1</span>
<span style="color: #002200;">&#41;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view7 setTitleShadowColor<span style="color: #002200;">:</span>NSNamedColorSpace System controlColor forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
&nbsp;
UISwitch <span style="color: #002200;">*</span>view8 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UISwitch alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">103.0</span>, <span style="color: #2400d9;">103.0</span>, <span style="color: #2400d9;">94.0</span>, <span style="color: #2400d9;">27.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
view8.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">103.0</span>, <span style="color: #2400d9;">103.0</span>, <span style="color: #2400d9;">94.0</span>, <span style="color: #2400d9;">27.0</span><span style="color: #002200;">&#41;</span>;
view8.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.000</span>;
view8.autoresizingMask <span style="color: #002200;">=</span> UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
view8.clearsContextBeforeDrawing <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view8.clipsToBounds <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view8.contentHorizontalAlignment <span style="color: #002200;">=</span> UIControlContentHorizontalAlignmentCenter;
view8.contentMode <span style="color: #002200;">=</span> UIViewContentModeScaleToFill;
view8.contentVerticalAlignment <span style="color: #002200;">=</span> UIControlContentVerticalAlignmentCenter;
view8.enabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view8.hidden <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view8.highlighted <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view8.multipleTouchEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view8.on <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
view8.opaque <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view8.selected <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
view8.tag <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
view8.userInteractionEnabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>view4 addSubview<span style="color: #002200;">:</span>view6<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view4 addSubview<span style="color: #002200;">:</span>view7<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>view4 addSubview<span style="color: #002200;">:</span>view8<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Comme vous pouvez le voir, c&#8217;est très verbeux. C&#8217;est parce que l&#8217;outil ne détecte pas les valeurs qui sont là par défaut. Donc dans le doute, il génère toutes les propriétés. A noter aussi que les valeurs des propriétés <code>UIImage</code>, <code>NSLocale</code> et <code>NSTimeZone</code> ne sont pas sorties dans le code. En effet, ibtool (sur lequel nib2objc se base) ne les supporte pas. </p>
<p><a href="http://kosmaczewski.net/projects/nib2objc/">Plus d&#8217;informations sur la page du projet nib2objc du site d&#8217;Adrian Kosmaczewski</a>. <!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/735-nib2objc-ou-comment-convertir-un-nib-en-code-objectif-c/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Comparer deux NSDate</title>
		<link>http://webd.fr/637-comparer-deux-nsdate</link>
		<comments>http://webd.fr/637-comparer-deux-nsdate#comments</comments>
		<pubDate>Fri, 05 Jun 2009 08:49:47 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=637</guid>
		<description><![CDATA[Il est souvent utile de comparer deux NSDate dans le temps. Il s&#8217;agit de savoir si une date est antérieure ou postérieure à une autre. Le mécanisme offert par Apple pour les comparaisons de NSDate n&#8217;est pas des plus pratiques. &#8230; <a href="http://webd.fr/637-comparer-deux-nsdate">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Il est souvent utile de comparer deux <code>NSDate</code> dans le temps. Il s&#8217;agit de savoir si une date est antérieure ou postérieure à une autre. Le mécanisme offert par Apple pour les comparaisons de <code>NSDate</code> n&#8217;est pas des plus pratiques. Il faut utiliser la méthode <code>compare:(NSDate*)date</code> qui retourne un objet de type <code>NSComparisonResult</code>. </p>
<p>Cette méthode est une méthode standard de Foundation. Elle permet, grâce à des résultats de comparaison standardisés d&#8217;effectuer un tri sur des objets de façon générique. Oui, mais Apple ne prévoit pas de méthodes un peu plus <i>haut niveau</i> pour les simples comparaisons de dates. Voilà pourquoi j&#8217;ai écrit rapidement une addition de la classe <code>NSDate</code> permettant de comparer deux dates intuitivement. Les voici:<br />
<span id="more-637"></span></p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p637code7'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6377"><td class="code" id="p637code7"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  NSDateAdditions.h</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Julien on 28/05/09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 Julien Quéré - Webd.fr. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a> <span style="color: #002200;">&#40;</span>NSDateAdditions<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isLaterThanOrEqualTo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isEarlierThanOrEqualTo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isLaterThan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date;
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isEarlierThan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date;
<span style="color: #a61390;">@end</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #11740a; font-style: italic;">//  NSDateAdditions.m</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Julien on 28/05/09.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2009 Julien Quéré - Webd.fr. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
&nbsp;
<span style="color: #6e371a;">#import &quot;NSDateAdditions.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a> <span style="color: #002200;">&#40;</span>NSDateAdditions<span style="color: #002200;">&#41;</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> isLaterThanOrEqualTo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self compare<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NSOrderedAscending<span style="color: #002200;">&#41;</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> isEarlierThanOrEqualTo<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">!</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self compare<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NSOrderedDescending<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isLaterThan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self compare<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NSOrderedDescending<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> isEarlierThan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>date <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self compare<span style="color: #002200;">:</span>date<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> NSOrderedAscending<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>L&#8217;utilisation, quant à elle est on-ne-peut plus simple:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p637code8'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6378"><td class="code" id="p637code8"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Compare a NSDate (maDate) and the actual date</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>maDate isEarlierThanOrEqualTo<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/"><span style="color: #400080;">NSDate</span></a> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;It's earlier or equal&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">else</span>
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;It's later&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/637-comparer-deux-nsdate/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Redimensionner une UIImage</title>
		<link>http://webd.fr/320-redimensionner-une-uiimage</link>
		<comments>http://webd.fr/320-redimensionner-une-uiimage#comments</comments>
		<pubDate>Fri, 27 Feb 2009 15:14:04 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=320</guid>
		<description><![CDATA[Dans certains cas, il peut être utile de redimensionner une UIImage. En effet, même si UIImageView gère très bien le redimensionnement, on peut vouloir exporter une image dans une certaine taille. De même, il n&#8217;est pas forcement utile de stocker &#8230; <a href="http://webd.fr/320-redimensionner-une-uiimage">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Dans certains cas, il peut être utile de redimensionner une <code>UIImage</code>. En effet, même si <code>UIImageView</code> gère très bien le redimensionnement, on peut vouloir exporter une image dans une certaine taille. De même, il n&#8217;est pas forcement utile de stocker (en mémoire ou bien en dur) une image de grande taille si c&#8217;est pour l&#8217;afficher réduite par la suite. </p>
<p>Pour redimensionner une image, c&#8217;est plutôt simple ! L&#8217;idée est de créer un contexte d&#8217;image de la taille souhaitée (ici 40*40 pixels). Dans ce contexte, en dessine l&#8217;image à redimensionner dans la taille souhaitée. Il ne reste alors plus qu&#8217;à récupérer l&#8217;image redimensionnée avec un <code>UIGraphicsGetImageFromCurrentImageContext</code>. Pour finir, on pensera bien à fermer le contexte courant. </p>
<p>Voici le code final:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p320code10'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p32010"><td class="code" id="p320code10"><pre class="objc" style="font-family:monospace;">CGSize size <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">40</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span>;
UIGraphicsBeginImageContext<span style="color: #002200;">&#40;</span> size <span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#91;</span>avatarImage drawInRect<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span>,size.width,size.height<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
avatarImage <span style="color: #002200;">=</span> UIGraphicsGetImageFromCurrentImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
UIGraphicsEndImageContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/320-redimensionner-une-uiimage/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Désactiver les NSLog en release</title>
		<link>http://webd.fr/307-desactiver-les-nslog-en-release</link>
		<comments>http://webd.fr/307-desactiver-les-nslog-en-release#comments</comments>
		<pubDate>Fri, 20 Feb 2009 15:28:09 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=307</guid>
		<description><![CDATA[Le NSLog est l&#8217;outil de base dans le développement et le déboguage d&#8217;application Cocoa (donc Cocoa Touch). Seulement, l&#8217;utilisation de NSLog n&#8217;est pas si anodine qu&#8217;elle peut paraître … Le fait d&#8217;envoyer une chaine de caractère à la sortie console &#8230; <a href="http://webd.fr/307-desactiver-les-nslog-en-release">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Le <code>NSLog</code> est l&#8217;outil de base dans le développement et le déboguage d&#8217;application Cocoa (donc Cocoa Touch). Seulement, l&#8217;utilisation de <code>NSLog</code> n&#8217;est pas si anodine qu&#8217;elle peut paraître … Le fait d&#8217;envoyer une chaine de caractère à la sortie console consomme des ressources. </p>
<p>Certes, c&#8217;est peu de choses, mais quand un <code>NSLog</code> est inclus dans une boucle, se déclenche à chaque <code>touchesMoved:</code> (par exemple), il commence à ralentir très sérieusement le fonctionnement de l&#8217;application. Sans compter qu&#8217;une fois l&#8217;application distribuée, il est possible pour l&#8217;utilisateur final de lire ce qui se trouve dans les logs … Ce qui n&#8217;est pas forcement voulu pour le développeur. </p>
<p>L&#8217;idée serait donc, quand on est en configuration de compilation « release », de désactiver (automatiquement les <code>NSLog</code>). <span id="more-307"></span>Pour ce faire, on va simplement ajouter le code suivant dans <code>leNomDeLApplication_Prefix.pch</code> (il s&#8217;agit simplement d&#8217;un fichier qui est inclus dans tous les fichiers d&#8217;un projet):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p307code12'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p30712"><td class="code" id="p307code12"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#ifndef DEBUG</span>
<span style="color: #6e371a;">#define NSLog //</span>
<span style="color: #6e371a;">#endif</span></pre></td></tr></table></div>

<p>Le code veut simplement dire: <cite>Si <code>DEBUG</code> n&#8217;est pas activé, on remplace les <code>NSLog</code> par des commentaires</cite>. On pensera donc à ajouter un flag <code>DEBUG</code> dans <code>GCC_PREPROCESSOR_DEFINITIONS</code> de la configuration de compilation dans le mode <code>Debug</code>. Notez que, par défaut, <code>GCC_PREPROCESSOR_DEFINITIONS</code> n&#8217;existe pas, il faudra donc le créer à la main. </p>
<p><a href="http://webd.fr/wp-content/uploads/2009/02/disablenslogscreen.png" rel="lightbox[307]" title="disablenslogscreen"><img src="http://webd.fr/wp-content/uploads/2009/02/disablenslogscreen-281x300.png" alt="disablenslogscreen" title="disablenslogscreen" width="281" height="300" class="aligncenter size-medium wp-image-308" /></a></p>
<p>Voilà, c&#8217;est fini <img src='http://webd.fr/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Notez tout de même que cette façon de faire a une limitation: il faut que les <code>NSLog</code> soient définis sur une seule ligne. Mais bon, ça l&#8217;est dans 99% des cas ! <!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/307-desactiver-les-nslog-en-release/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iphonelite3: Une nouvelle librairie SQLite pour iPhone</title>
		<link>http://webd.fr/288-iphonelite3-une-nouvelle-librairie-sqlite-pour-iphone</link>
		<comments>http://webd.fr/288-iphonelite3-une-nouvelle-librairie-sqlite-pour-iphone#comments</comments>
		<pubDate>Thu, 29 Jan 2009 20:32:18 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[SQLite]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=288</guid>
		<description><![CDATA[Je viens de découvrir aujourd&#8217;hui une nouvelle librairie permettant de simplifier l&#8217;utilisation de SQLite3 sur iPhone: Iphonelite3. L&#8217;idée global du projet est de ne pas avoir à coder une seule ligne de SQL dans son application. Il s&#8217;agit tout bêtement &#8230; <a href="http://webd.fr/288-iphonelite3-une-nouvelle-librairie-sqlite-pour-iphone">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Je viens de découvrir aujourd&#8217;hui une nouvelle librairie permettant de simplifier l&#8217;utilisation de SQLite3 sur iPhone: Iphonelite3. L&#8217;idée global du projet est de ne pas avoir à coder une seule ligne de SQL dans son application. Il s&#8217;agit tout bêtement de faire de la persistance d&#8217;objet. Là où le concept est intéressant, c&#8217;est qu&#8217;il ne se base pas sur des objets héritant d&#8217;une quelconque implémentation de serialisation. </p>
<p>Non, l&#8217;idée est plus axée « hibernate » (que l&#8217;on trouve avec Java). Vous passez votre objet au gestionnaire de base de données et il s&#8217;occupe de tout le reste. Je ne vais pas continuer à faire l&#8217;article de Iphonelite3, son auteur le fait très bien <a href="http://iphonelite3.wordpress.com/">sur son blog dédié</a>. Notez que le projet est, pour le moment, en phase <cite>alpha</cite> et ne doit donc pas être utilisé en phase de production. Mais c&#8217;est à regarder avec attention. </p>
<p>Au passage, je rappellerais ce qui se fait sur ce segment en ce moment. Il y a les projets <a href="http://code.google.com/p/flycode/source/browse/trunk/fmdb">FMDB</a> (que j&#8217;utilise dans deux de mes projets), <a href="http://code.google.com/p/sqlitepersistentobjects/">SQLitePersistentObjects</a> (que je n&#8217;ai pas pu utiliser dans mon dernier projet car il gère mal les <code>NSMutableDictionary</code>) et <a href="http://code.google.com/p/entropydb/">EntropyDB</a>.<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/288-iphonelite3-une-nouvelle-librairie-sqlite-pour-iphone/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Une UITableView triée par ordre alphabétique</title>
		<link>http://webd.fr/237-une-uitableview-triee-par-ordre-alphabetique</link>
		<comments>http://webd.fr/237-une-uitableview-triee-par-ordre-alphabetique#comments</comments>
		<pubDate>Mon, 26 Jan 2009 07:08:57 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=237</guid>
		<description><![CDATA[L&#8217;UITableView est un outil idéal pour afficher une certaine quantité de données à l&#8217;utilisateur. Seulement voilà, quand cette quantité de données devient grande, il peut être intéressant de permettre à l&#8217;utilisateur d&#8217;accéder rapidement à l&#8217;information qu&#8217;il souhaite. Une des solutions &#8230; <a href="http://webd.fr/237-une-uitableview-triee-par-ordre-alphabetique">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>L&#8217;<code>UITableView</code> est un outil idéal pour afficher une certaine quantité de données à l&#8217;utilisateur. Seulement voilà, quand cette quantité de données devient grande, il peut être intéressant de permettre à l&#8217;utilisateur d&#8217;accéder rapidement à l&#8217;information qu&#8217;il souhaite.</p>
<p>Une des solutions est d&#8217;utiliser le tri par ordre alphabétique. C&#8217;est l&#8217;idéal pour l&#8217;affichage de contacts (par exemple). On peut même aller plus loin. Pourquoi simplement se limiter à un tri par ordre alphabétique ? Pourquoi ne pas proposer une catégorisation des entrées en prenant en compte la première lettre de la propriété qui nous intéresse. C&#8217;est ce que nous allons voir dans ce billet. Au final, nous arriverons à ce résultat:</p>
<p><a href="http://webd.fr/wp-content/uploads/2009/01/alphabeticaltableviewfinalscreen.png" rel="lightbox[237]" title="alphabeticaltableviewfinalscreen"><img src="http://webd.fr/wp-content/uploads/2009/01/alphabeticaltableviewfinalscreen-161x300.png" alt="alphabeticaltableviewfinalscreen" title="alphabeticaltableviewfinalscreen" width="161" height="300" class="aligncenter size-medium wp-image-274" /></a><br />
<span id="more-237"></span></p>
<p>On commence par créer notre projet dans Xcode et on lui ajoute un <code>TableViewController</code>. Pour le moment, on n&#8217;y touche pas. Ensuite, on créée le <code>xib</code> des cellules avec Interface Builder (pour plus de détails sur la manipulation de <code>UITableViewCell</code> avec Interface Builder, voir <a href="http://webd.fr/141-creer-une-uitableviewcell-personnalisee-a-partir-dinterface-builder"><cite>Créer une UITableViewCell personnalisée à partir d’Interface Builder</cite></a>). Maintenant, faisons une structure pour nos données. Ce sera une simple classe contenant 4 propriétés <code>NSString</code>: <code>firstname</code>, <code>lastname</code>, <code>age</code> et <code>sex</code>. Ce qui donne tout simplement:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p237code18'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23718"><td class="code" id="p237code18"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> Person <span style="color: #002200;">:</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/"><span style="color: #400080;">NSObject</span></a> <span style="color: #002200;">&#123;</span>
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>firstname;
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>lastname;
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>age;
    <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>sex;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>On rajoute aussi les méthodes d&#8217;initialisation et de désallocation de l&#8217;objet:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p237code19'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23719"><td class="code" id="p237code19"><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> initWithLastName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>mlastname andFirstname<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>mfirstname andSex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>msex andAge<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>mage <span style="color: #002200;">&#123;</span>
	lastname <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span>mlastname<span style="color: #002200;">&#93;</span>;
	firstname <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span>mfirstname<span style="color: #002200;">&#93;</span>;
	sex <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span>msex<span style="color: #002200;">&#93;</span>;
	age <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span>mage<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>super init<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;">void</span><span style="color: #002200;">&#41;</span> dealloc <span style="color: #002200;">&#123;</span>	
	<span style="color: #002200;">&#91;</span>lastname release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>firstname release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>sex release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>age 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></pre></td></tr></table></div>

<p>Toute la partie <cite>manipulation de données</cite> sera gérée par <code>PersonDataSource</code>. Il s&#8217;agit d&#8217;une classe contenant:</p>
<ul>
<li>un <code>NSArray</code> (<code>personList</code>) qui contiendra notre liste de <code>Person</code> brute (c&#8217;est à dire non-triée)</li>
<li>un <code>NSMutableDictionary</code> (<code>personListAlphabeticalDictionary</code>) qui contiendra nos <code>Person</code> triées par ordre alphabétique. Chaque entrée du dictionnaire aura pour clef la première lettre du nom des <code>Person</code> et en valeur, le tableau de <code>Person</code> correspondant.</li>
<li>un <code>NSArray</code> (<code>personNameFirstLeterIndexArray</code>) qui contiendra la liste de toutes les premières lettres de noms disponibles. En effet, il se peut que l&#8217;on n&#8217;ai aucune entrée avec comme première lettre <cite>Z</cite> (par exemple).</li>
</ul>
<p>Du coté des méthodes, ça donne cela: </p>
<ul>
<li><code>- (void) populate</code>: cette méthode ne fait que récupérer nos objets <code>Person</code>. Dans l&#8217;exemple, j&#8217;ajoute les objets à la main, mais on peut tout à fait remplacer ça par une récupération de données dans un fichier XML ou bien une base de données.</li>
<li><code>- (void) populatePersonListAlphabeticalDictionary</code>: cette méthode va aller chercher le contenu de <code>personList</code> et générer <code>personListAlphabeticalDictionary</code> comme convenu. Pour finir, la méthode va appeler <code>sortPersonInitialLetterIndexes</code>.</li>
<li><code>- (void) sortPersonInitialLetterIndexes</code>: cette méthode va générer <code>personNameIndexArray</code> en récupérant toutes les clefs de <code>personListAlphabeticalDictionary</code> (triées alphabétiquement). Puis pour chacune des clefs, <code>presortPersonListAlphabeticalDictionaryForInitialLetter</code></li>
<li><code>- (void)presortPersonListAlphabeticalDictionaryForInitialLetter:(NSString *)aKey</code> va (pour une clef <code>aKey</code> donnée), trier le contenu du <code>NSArray</code> correspondant dans le <code>personListAlphabeticalDictionary</code>.</li>
<li><code>- (NSArray *)personsWithInitialLetter:(NSString*)aKey</code>: cette méthode retourne un <code>NSArray</code> avec toutes les <code>Person</code> ayant pour première lettre <code>aKey</code>.</li>
<li><code>- (Person *) personForIndexPath:(NSIndexPath *)indexPath</code> retourne l&#8217;objet <code>Person</code> correspondant au <code>NSIndexPath</code> passé en paramètre. Cette méthode sera appelée par le <code>UITableViewController</code>.</li>
</ul>
<p>Au final, on obtient ceci:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p237code20'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23720"><td class="code" id="p237code20"><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: #002200;">&#91;</span>self populate<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self populatePersonListAlphabeticalDictionary<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span>
<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>
&nbsp;
	<span style="color: #002200;">&#91;</span>personList release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personListAlphabeticalDictionary release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personNameIndexArray 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;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> populate <span style="color: #002200;">&#123;</span>		
	personList <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span style="color: #400080;">NSMutableArray</span></a> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Léponge&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bob&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homme&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;21&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Daniel&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Jack&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Whiskey&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;12&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fontaine&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Claire&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Femme&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;89&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Charues&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Vielles&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Festival&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;125&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bull&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Red&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Boisson&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;2&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bonvoisin&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bernie&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homme&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;55&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Jobs&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Steeve&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homme&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;80&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Damien&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Francois&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Humoriste&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;35&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Gates&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bill&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Homme&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;50&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Laporte&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bernard&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Rugbyman&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;32&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>personList addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Person alloc<span style="color: #002200;">&#93;</span> initWithLastName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Chabal&quot;</span> andFirstname<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sebastien&quot;</span> andSex<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Mythe&quot;</span> andAge<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;36&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;	
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> populatePersonListAlphabeticalDictionary <span style="color: #002200;">&#123;</span>
	personListAlphabeticalDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/"><span style="color: #400080;">NSMutableDictionary</span></a> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span>Person <span style="color: #002200;">*</span>currentPerson <span style="color: #a61390;">in</span> personList<span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
&nbsp;
		<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>firstLetter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>currentPerson.lastname substringToIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
		<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span style="color: #400080;">NSMutableArray</span></a> <span style="color: #002200;">*</span>existingArray;		
		<span style="color: #11740a; font-style: italic;">/* Si on a déjà un array de Person pour cette première lettre*/</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>existingArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>personListAlphabeticalDictionary valueForKey<span style="color: #002200;">:</span>firstLetter<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;">/* On y ajoute la Person en cours*/</span>
			<span style="color: #002200;">&#91;</span>existingArray addObject<span style="color: #002200;">:</span>currentPerson<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>	<span style="color: #11740a; font-style: italic;">/*Sinon, on créée l'array, on y ajoute la Person courante et on ajoute l'array au dictionnaire*/</span>
		<span style="color: #002200;">&#123;</span>
			<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span style="color: #400080;">NSMutableArray</span></a> <span style="color: #002200;">*</span>tempArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span style="color: #400080;">NSMutableArray</span></a> array<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>tempArray addObject<span style="color: #002200;">:</span>currentPerson<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>personListAlphabeticalDictionary setObject<span style="color: #002200;">:</span>tempArray forKey<span style="color: #002200;">:</span>firstLetter<span style="color: #002200;">&#93;</span>;			
		<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* On génère personNameIndexArray et on trie chaque array de personListAlphabeticalDictionary*/</span>
	<span style="color: #002200;">&#91;</span>self sortPersonInitialLetterIndexes<span style="color: #002200;">&#93;</span>;
&nbsp;
<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> sortPersonInitialLetterIndexes <span style="color: #002200;">&#123;</span>
	personNameIndexArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/"><span style="color: #400080;">NSMutableArray</span></a> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">/* On récupère toutes les premières lettres et on les trie en ignorant la case*/</span>
	personNameIndexArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>personListAlphabeticalDictionary allKeys<span style="color: #002200;">&#93;</span> sortedArrayUsingSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>localizedCaseInsensitiveCompare<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span> eachNameIndex <span style="color: #a61390;">in</span> personNameIndexArray<span style="color: #002200;">&#41;</span> 
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self presortPersonListAlphabeticalDictionaryForInitialLetter<span style="color: #002200;">:</span>eachNameIndex<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>presortPersonListAlphabeticalDictionaryForInitialLetter<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aKey <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">/* Pour chaque array à la clef aKey dans le dictionnaire, on trie le contenu de l'array alphabétiquement sur la propriété lastname en ignorant la case*/</span>
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/"><span style="color: #400080;">NSSortDescriptor</span></a> <span style="color: #002200;">*</span>nameDescriptor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/"><span style="color: #400080;">NSSortDescriptor</span></a> alloc<span style="color: #002200;">&#93;</span> initWithKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastname&quot;</span>
																   ascending<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span>
																	selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>localizedCaseInsensitiveCompare<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;	
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> <span style="color: #002200;">*</span>descriptors <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> arrayWithObject<span style="color: #002200;">:</span>nameDescriptor<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>personListAlphabeticalDictionary objectForKey<span style="color: #002200;">:</span>aKey<span style="color: #002200;">&#93;</span> sortUsingDescriptors<span style="color: #002200;">:</span>descriptors<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>nameDescriptor release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>Person <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> personForIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span style="color: #400080;">NSIndexPath</span></a> <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;">/* On récupère la lettre qui a un ID correspondant à la section de l'indexPath */</span>
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> <span style="color: #002200;">*</span>personsWithInitLetter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self personsWithInitialLetter<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>personNameIndexArray objectAtIndex<span style="color: #002200;">:</span>indexPath.section<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">/* Et on renvoie la Person correspondant au row*/</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>personsWithInitLetter objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;	
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>personsWithInitialLetter<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aKey <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">/* On renvoie la liste des Person ayant pour première lettre aKey*/</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>personListAlphabeticalDictionary objectForKey<span style="color: #002200;">:</span>aKey<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Maintenant que notre source de données est prête, il n&#8217;y a plus qu&#8217;à mettre en place la TableView en elle-même. La première chose est d&#8217;ajouter un <code>personDataSource</code> comme variable d&#8217;instance et de l&#8217;initialiser (via <code>[[PersonDataSource alloc]init];</code>). Ensuite, on attaque le <code><br />
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code>. Le fonctionement est traditionnel. La seule subtilité, c&#8217;est qu&#8217;on va récupérer l&#8217;objet <code>Person</code> via <code>[personDataSource personForIndexPath:indexPath]</code>. </p>
<p>Pour le nombre de sections dans la table, c&#8217;est simple: il y en aura autant que de première lettres. Donc, c&#8217;est tout naturellement que <code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView</code> renvera [[personDataSource personNameIndexArray] count]</code>. Pour le nombre de lignes dans chaque section, c'est le nombre de <code>Person</code> ayant pour première lettre celle qui correspond à la section courante. C'est pourquoi <code><br />
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section</code> retourne <code>return [[self miniContactsWithInitialLetter:[miniContactNameIndexArray  objectAtIndex:section]] count];</code>. On obtient donc ce code pour le <code>UITableViewController</code>:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p237code21'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23721"><td class="code" id="p237code21"><pre class="objc" style="font-family:monospace;">&nbsp;
<span style="color: #a61390;">@implementation</span> AlphaTableViewController
<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>
		personDataSource <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>PersonDataSource alloc<span style="color: #002200;">&#93;</span>init<span style="color: #002200;">&#93;</span>;			
		self.tableView.rowHeight <span style="color: #002200;">=</span> <span style="color: #2400d9;">125</span>;		
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
&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><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span style="color: #400080;">NSIndexPath</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">static</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyCellIdent&quot;</span>;
&nbsp;
    TableCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>TableCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</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>
    	UIViewController <span style="color: #002200;">*</span>c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TableViewCell&quot;</span>  bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
		cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>TableCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>c.view;
		<span style="color: #002200;">&#91;</span>c release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
	Person <span style="color: #002200;">*</span>currentPerson <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>personDataSource personForIndexPath<span style="color: #002200;">:</span>indexPath<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>cell.firstname setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>currentPerson firstname<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>cell.lastname setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>currentPerson lastname<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>cell.sex setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>currentPerson sex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;	
	<span style="color: #002200;">&#91;</span>cell.age setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>currentPerson age<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;	
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
&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: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>personDataSource personNameIndexArray<span style="color: #002200;">&#93;</span> 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>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: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>personDataSource personsWithInitialLetter<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>personDataSource personNameIndexArray<span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span>section<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Cela donne le résultat ci-dessous. C'est bien, mais il manque encore la liste des lettres sur la droite. C'est pas compliqué <img src='http://webd.fr/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .<br />
<a href="http://webd.fr/wp-content/uploads/2009/01/alphabeticaltableviewmiddlescreen.png" rel="lightbox[237]" title="alphabeticaltableviewmiddlescreen"><img src="http://webd.fr/wp-content/uploads/2009/01/alphabeticaltableviewmiddlescreen-161x300.png" alt="alphabeticaltableviewmiddlescreen" title="alphabeticaltableviewmiddlescreen" width="161" height="300" class="aligncenter size-medium wp-image-275" /></a></p>
<p>En fait, il faut être capable de répondre à deux questions posées à l'<code>UITableViewController</code>: <cite>quels sont les titres à afficher pour l'index de la TableView</cite> via <code><br />
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView</code> et <cite>Pour la section X, où faut-il scroller la TableView</cite> via<br />
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index</code>. Au final, il est très facile de répondre à ces questions dans l'architecture que l'on a mis en place. Voilà ce que ça donne dans le code:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p237code22'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23722"><td class="code" id="p237code22"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/"><span style="color: #400080;">NSArray</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>sectionIndexTitlesForTableView<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: #a61390;">return</span> <span style="color: #002200;">&#91;</span>personDataSource personNameIndexArray<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<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 sectionForSectionIndexTitle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>title atIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>index <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> index;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Et le résultat final ... Vous l'avez déjà vu plus haut. Si vous voulez, vous trouverez <a href='http://webd.fr/wp-content/uploads/2009/01/alphatable.zip'>ici une archive du projet complet (Xcode 3.1)</a>.<br />
<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/237-une-uitableview-triee-par-ordre-alphabetique/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Créer une UITableViewCell personnalisée à partir d’Interface Builder: exemple complet</title>
		<link>http://webd.fr/224-creer-uitableviewcell-personnalisee-interface-builder-exemple</link>
		<comments>http://webd.fr/224-creer-uitableviewcell-personnalisee-interface-builder-exemple#comments</comments>
		<pubDate>Fri, 09 Jan 2009 22:51:47 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=224</guid>
		<description><![CDATA[Il y a peu, j&#8217;exposais ma solution pour Créer une UITableViewCell personnalisée à partir d&#8217;Interface Builder. Suite au commentaire de Bruno Da Silva, je vous laisse une version complètement fonctionnelle de l&#8217;implémentation de cette méthode. Il s&#8217;agit d&#8217;une TableView qui &#8230; <a href="http://webd.fr/224-creer-uitableviewcell-personnalisee-interface-builder-exemple">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Il y a peu, j&#8217;exposais ma solution pour <cite><a href="http://webd.fr/141-creer-une-uitableviewcell-personnalisee-a-partir-dinterface-builder">Créer une UITableViewCell personnalisée à partir d&#8217;Interface Builder</a></cite>. Suite au <a href="http://webd.fr/141-creer-une-uitableviewcell-personnalisee-a-partir-dinterface-builder#comment-86">commentaire de Bruno Da Silva</a>, je vous laisse une version complètement fonctionnelle de l&#8217;implémentation de cette méthode. </p>
<p>Il s&#8217;agit d&#8217;une TableView qui va chercher les dernières photos Flickr avec pour tag « <cite>xmas</cite> ». Pourquoi donner un exemple aussi compliqué ? Tout simplement parce que je n&#8217;ai <b>vraiment</b> pas le temps de faire un exemple complet en ce moment et je voulais publier un exemple rapidement. J&#8217;ai donc récupéré rapidement un exemple de code que j&#8217;ai fais en interne pour le boulot. </p>
<p>La partie de code qui va chercher les images sur FlickR n&#8217;est pas de moi. L&#8217;auteur n&#8217;ayant pas laissé d&#8217;informations le concernant dans le code, je ne pourrais plus vous dire qui c&#8217;est (et ça me désole vraiment). </p>
<p>Voici l&#8217;archive contenant le projet Xcode complet (toute l&#8217;intelligence concernant la méthode pour l&#8217;<code>UITableViewCell</code> se trouve dans <code>MyTableViewController.m</code> à la méthode <code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code>): télécharger <a href='http://webd.fr/wp-content/uploads/2009/01/mytableviewdemo.zip'>MyTableViewDemo.zip</a>.<br />
<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/224-creer-uitableviewcell-personnalisee-interface-builder-exemple/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Réduire la hauteur d&#8217;une UITableView</title>
		<link>http://webd.fr/158-reduire-la-hauteur-dune-uitableview</link>
		<comments>http://webd.fr/158-reduire-la-hauteur-dune-uitableview#comments</comments>
		<pubDate>Mon, 05 Jan 2009 08:23:33 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=158</guid>
		<description><![CDATA[Prenons un cas simple: on a une UIView qui contient une simple UITableView (avec le UITableViewController qui va bien) ainsi qu&#8217;une UIToolBar en bas. On se retrouve avec un léger problème: la UITableView ne se redimensionne pas automatiquement en hauteur. &#8230; <a href="http://webd.fr/158-reduire-la-hauteur-dune-uitableview">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Prenons un cas simple: on a une <code>UIView</code> qui contient une simple <code>UITableView</code> (avec le <code>UITableViewController</code> qui va bien) ainsi qu&#8217;une <code>UIToolBar</code> en bas. On se retrouve avec un léger problème: la <code>UITableView</code> ne se redimensionne pas automatiquement en hauteur. Ainsi, les derniers éléments se retrouvent « cachés » par la barre. Voici un exemple fait à la va-vite avec Interface Builder (la <code>UIToolBar</code> a été volontairement mise en transparence pour illustrer le problème):</p>
<p><a href="http://webd.fr/wp-content/uploads/2008/12/uitableviewsizeproblem.png" rel="lightbox[158]" title="uitableviewsizeproblem"><img class="aligncenter size-medium wp-image-159" title="uitableviewsizeproblem" src="http://webd.fr/wp-content/uploads/2008/12/uitableviewsizeproblem-206x299.png" alt="uitableviewsizeproblem" width="206" height="299" /></a></p>
<p>Le souci, c&#8217;est qu&#8217;il n&#8217;est pas possible de préciser dans le code la taille d&#8217;une <code>UITableView</code>. Après avoir essayé plusieurs solutions (dont certaines était plutôt farfelue), j&#8217;en ai retenu une. C&#8217;est celle qui m&#8217;a semblé la plus propre, simple et efficace.<br />
<span id="more-158"></span><br />
Il s&#8217;agit tout simplement de créer une vue vide de la taille de la <code>UIToolBar</code> (par défaut: 44 pixels). Puis de la mettre en tant que <code>tableFooterView</code> (une vue qui n&#8217;est affichée que tout en bas de la <code>UITableView</code>) sur la <code>UITableView</code> courante. L&#8217;idéal c&#8217;est de faire ça directement dans la phase d&#8217;initialisation (dans le <code>UITableViewController</code>). Concrètement ça donne ça:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p158code24'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p15824"><td class="code" id="p158code24"><pre class="objc" style="font-family:monospace;">self.tableView.tableFooterView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">44</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/158-reduire-la-hauteur-dune-uitableview/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Créer une UITableViewCell personnalisée à partir d&#8217;Interface Builder</title>
		<link>http://webd.fr/141-creer-une-uitableviewcell-personnalisee-a-partir-dinterface-builder</link>
		<comments>http://webd.fr/141-creer-une-uitableviewcell-personnalisee-a-partir-dinterface-builder#comments</comments>
		<pubDate>Fri, 26 Dec 2008 09:21:35 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=141</guid>
		<description><![CDATA[Le problème La TableView est un composant essentiel de l&#8217;HIM de l&#8217;iPhone très simple à développer pour des besoins simples. Clairement, si on doit afficher à l&#8217;utilisateur des cellules avec une seule ligne de texte, ça se fait les doigts &#8230; <a href="http://webd.fr/141-creer-une-uitableviewcell-personnalisee-a-partir-dinterface-builder">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h1>Le problème</h1>
<p>La TableView est un composant essentiel de l&#8217;HIM de l&#8217;iPhone très simple à développer pour des besoins simples. Clairement, si on doit afficher à l&#8217;utilisateur des cellules avec une seule ligne de texte, ça se fait les doigts dans le nez.</p>
<p>Là où les choses se corsent, c&#8217;est quand il faut créer des cellules personnalisées. Avec, par exemple, deux <code>UILabel</code> et une <code>UIImageView</code>. En effet, il n&#8217;existe pas de méthode pour charger un nib (comme l&#8217;habituel <code>initWithNibName:</code>). Ainsi, on se retrouve forcé à créer tout nos éléments et à les placer à la main dans le code. C&#8217;est loin d&#8217;être quelque chose d&#8217;agréable, simple et rapide à faire. Sans compter qu&#8217;au moindre changement de design de la cellule, on perd un temps fou.</p>
<h1>La solution</h1>
<p>Non, l&#8217;idéal c&#8217;est de pouvoir utiliser Interface Builder pour construire ses cellules&#8230; Après quelques essais, recherches et erreurs, voici la solution que j&#8217;ai trouvé (et que j&#8217;utilise):</p>
<p><span id="more-141"></span></p>
<p>On commence par créer une nouvelle interface dans IB. On y ajoute une TableCell (au passage, le nib ne doit contenir que des TableCell&#8217;s !). Dans la TableCell, on ajoute les éléments dont on a besoin (ici, deux <code>UILabel</code> et un <code>UIImageView</code>). On définit la <cite>Class Identity</cite> et les <cite>Class Outlets</cite> qui vont bien. On finit par un <cite>Write Classfile</cite> et on a notre nib et l&#8217;<code>UITableViewCell</code> dans le projet.<br />
<a href="http://webd.fr/wp-content/uploads/2008/12/tableviewcellxibscreen.jpg" rel="lightbox[141]" title="tableviewcellxibscreen"><img src="http://webd.fr/wp-content/uploads/2008/12/tableviewcellxibscreen-300x233.jpg" alt="tableviewcellxibscreen" title="tableviewcellxibscreen" width="300" height="233" class="aligncenter size-medium wp-image-150" /></a></p>
<p>Maintenant, il faut s&#8217;occuper de l&#8217;<code>UITableViewController</code>. L&#8217;utilisation d&#8217;un nib ne change absolument rien au fonctionnement habituel. Ce n&#8217;est qu&#8217;à la création de la cellule (dans <code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code>) que cela change un peu. Voilà ce que ça donne:</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p141code26'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14126"><td class="code" id="p141code26"><pre class="objc" style="font-family:monospace;"><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><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSIndexPath_Class/"><span style="color: #400080;">NSIndexPath</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyCellIdent&quot;</span>;
    MyTableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>MyTableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</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>
    	<span style="color: #11740a; font-style: italic;">// On initialise un ViewController à partir du Nib qui a été fait</span>
    	UIViewController <span style="color: #002200;">*</span>c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyTableViewCell&quot;</span>  bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// On récupère sa vue et on la caste dans le type de notre TableViewCell</span>
	cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>MyTableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>c.view;
	<span style="color: #002200;">&#91;</span>c release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #11740a; font-style: italic;">// Ensuite, ça reste traditionnel</span>
    cell.label1.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>myObjects objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span> name<span style="color: #002200;">&#93;</span>;
    cell.label2.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>myObjects objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span> desc<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>cell.uIImageView  setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>myObjects objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span> image<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Les développeurs avisés auront très certainement remarqué un petit détail qui a son importance: on ne définit jamais (lors de la création de la cellule) son <code>CellIdentifier</code>, ainsi la réutilisation de cellules devient impossible (ce qui est suicidaire en terme de mémoire). En fait, celui-ci se définit dans Interface Builder. Dans les propriétés de votre TableViewCell, il suffit de mettre votre <code>CellIndetifier</code> dans <cite>Identifier</cite>. Ici, on mettra donc <cite> MyCellIdent</cite>.</p>
<p>Vous voilà maintenant avec des TableViewCell générée à partir d&#8217;Interface Builder en gardant possible le mécanisme de ré-utilisation de cellules. Ça, c&#8217;est des demies journées de gagnées sur vos projets (si ils comportent des cellules personnalisées bien évidement).</p>
<p><strong>Édition du 09/01/09:</strong><br />
Un exemple de code complet et opérationnel est disponible dans le billet <cite><a href="http://webd.fr/224-creer-uitableviewcell-personnalisee-interface-builder-exemple">Créer une UITableViewCell personnalisée à partir d’Interface Builder: exemple complet</a></cite><br />
<!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/141-creer-une-uitableviewcell-personnalisee-a-partir-dinterface-builder/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Afficher l&#8217;image d&#8217;un contact de l&#8217;AddressBook de l’iPhone</title>
		<link>http://webd.fr/128-image-contact-addressbook-iphone</link>
		<comments>http://webd.fr/128-image-contact-addressbook-iphone#comments</comments>
		<pubDate>Sat, 20 Dec 2008 10:43:56 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=128</guid>
		<description><![CDATA[Il y a peu, je parlais de la sérialisation d&#8217;un contact dans l&#8217;AddressBook de l&#8217;iPhone. Toujours dans la même veine, voyons comment afficher, dans une application, la photographie d&#8217;un contact de l&#8217;AddressBook. L&#8217;idée est de partir du ABRecordRef correspondant au &#8230; <a href="http://webd.fr/128-image-contact-addressbook-iphone">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Il y a peu, je parlais de la <a href="http://webd.fr/120-serialiser-identifiant-contact-iphone">sérialisation d&#8217;un contact dans l&#8217;AddressBook de l&#8217;iPhone</a>. Toujours dans la même veine, voyons comment afficher, dans une application, la photographie d&#8217;un contact de l&#8217;AddressBook. </p>
<p>L&#8217;idée est de partir du <code>ABRecordRef</code> correspondant au contact dont on souhaite afficher l&#8217;image. A partir de là, on vérifie que ce contact dispose bien d&#8217;une photo dans sa fiche (via <code>ABPersonHasImageData()</code>. Pour finir, on récupère les données brutes de l&#8217;image (<code> ABPersonCopyImageData()</code>) et on crée la <code>UIImage</code> qui va bien (via <code>initWithData:</code>). </p>
<p>Au final, voilà ce que ça donne: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p128code28'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12828"><td class="code" id="p128code28"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>ABPersonHasImageData<span style="color: #002200;">&#40;</span>recordRef<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> <span style="color: #002200;">*</span>myAvatarData <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/"><span style="color: #400080;">NSData</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> ABPersonCopyImageData<span style="color: #002200;">&#40;</span>recordRef<span style="color: #002200;">&#41;</span>;
	UIImage <span style="color: #002200;">*</span>avatarImage <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImage alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>myAvatarData<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>myAvatarData release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Après, il ne faut pas oublier de libérer <code>myAvatarData</code> puisqu&#8217;il est devenu inutile. Charge à vous ensuite d&#8217;afficher <code>avatarImage</code> dans le <code>UIImageView</code> qui va bien. Simple non ?</p>
<p><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/128-image-contact-addressbook-iphone/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Serialiser l&#8217;identifiant d&#8217;un contact dans l&#8217;AddressBook de l’iPhone</title>
		<link>http://webd.fr/120-serialiser-identifiant-contact-iphone</link>
		<comments>http://webd.fr/120-serialiser-identifiant-contact-iphone#comments</comments>
		<pubDate>Mon, 15 Dec 2008 11:16:21 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=120</guid>
		<description><![CDATA[Dans la série des billets concernant le développement sur iPhone, en voici un nouveau qui reste dans la veine du précédent (« Récupérer une adresse email dans le AddressBook de l’iPhone »). L&#8217;idée ce coup là, c&#8217;est de récupérer un &#8230; <a href="http://webd.fr/120-serialiser-identifiant-contact-iphone">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Dans la série des billets concernant le développement sur iPhone, en voici un nouveau qui reste dans la veine du précédent (« <a href="http://webd.fr/116-recuperer-une-adresse-email-dans-le-contactbook-de-liphone">Récupérer une adresse email dans le AddressBook de l’iPhone</a> »).</p>
<p>L&#8217;idée ce coup là, c&#8217;est de récupérer un identifiant d&#8217;un contact. Je m&#8217;explique: dans l&#8217;application que je développe je dois enregistrer (sur un serveur distant) des informations concernant un contact sélectionné par l&#8217;utilisateur. Clairement, il s&#8217;agit du nom, du prénom et de l&#8217;adresse email. Puis, dans un second temps, ces données peuvent revenir sur l&#8217;iPhone.</p>
<p>Seulement, il pourrait être intéressant de faire directement le lien, lors du retour de l&#8217;information, entre les personnes désignée par les données sur le serveur et l&#8217;entité qui la représente dans le AddressBook. Pourquoi ? Pour plusieurs raisons. Tout d&#8217;abord, imaginez que l&#8217;on modifie les informations concernant la personne (cas typique: on a fait une faute de frappe quand on a saisi son nom la première fois ou elle change d&#8217;adresse mail). Et bien si on est capable de détecter ce changement quand les données reviennent sur l&#8217;iPhone, on pourra répercuter les modifications sur le serveur. La seconde raison est qu&#8217;il peut être sympa de faire un lien vers la fiche de la  personne dans le AddressBook quand on affiche son nom dans l&#8217;application.</p>
<p>Quand on arrive dans le <code> peoplePickerNavigationController: shouldContinueAfterSelectingPerson</code> (donc que l&#8217;utilisateur a sélectionne un contact), on ne dispose que d&#8217;un objet de type <code>ABRecordRef</code>. Ce type d&#8217;objet est difficilement « sérialisable » &#8230; Comprenez par là qu&#8217;il serait difficile de le transformer en une chaine de caractère et de refaire l&#8217;opération inverse (pour le « dé-sérialiser »).</p>
<p>La solution est d&#8217;utiliser <code> ABRecordGetRecordID()</code> qui retourne un identifiant unique du contact dans l&#8217; AddressBook. Cette fonction renvoie un objet de type <code>ABRecordID </code> (qui n&#8217;est ni plus ni moins qu&#8217;un entier !). Au final, pour la convertir en chaine de caractère ça donne (<code>mPerson</code> étant notre objet de type <code>ABRecordRef</code>):</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p120code31'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12031"><td class="code" id="p120code31"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> contactId <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%u&quot;</span>, ABRecordGetRecordID<span style="color: #002200;">&#40;</span>mPerson<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Ensuite, l&#8217;opération inverse (récupérer le ABRecordRef a partir du de la chaine de caractère), c&#8217;est simple: </p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p120code32'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12032"><td class="code" id="p120code32"><pre class="objc" style="font-family:monospace;">ABRecordRef recordRef <span style="color: #002200;">=</span> ABAddressBookGetPersonWithRecordID<span style="color: #002200;">&#40;</span>addressBook, <span style="color: #002200;">&#91;</span>contactId intValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>Notez que dans le cas où le contact n&#8217;existe pas (plus), <code>ABAddressBookGetPersonWithRecordID()</code> renvoie <code>NULL</code>Voilà, maintenant on peut effectuer n&#8217;importe quelle opération comme si on venait de récupérer le contact à partir du <code> peoplePickerNavigationController</code>. </p>
<p><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/120-serialiser-identifiant-contact-iphone/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Récupérer une adresse email dans le ContactBook de l&#8217;iPhone</title>
		<link>http://webd.fr/116-recuperer-une-adresse-email-dans-le-contactbook-de-liphone</link>
		<comments>http://webd.fr/116-recuperer-une-adresse-email-dans-le-contactbook-de-liphone#comments</comments>
		<pubDate>Thu, 21 Aug 2008 18:42:43 +0000</pubDate>
		<dc:creator>Julien Quéré</dc:creator>
				<category><![CDATA[Pro]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Cocoa Touch]]></category>
		<category><![CDATA[Developpement]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://webd.fr/?p=116</guid>
		<description><![CDATA[J’avais dit que je parlerais ici même des petites astuces ou gros problèmes rencontrés lors du développement de mon application iPhone. L’objectif est assez clair : faire gagner quelques précieuses heures aux prochains qui rencontreront les mêmes problèmes que moi. &#8230; <a href="http://webd.fr/116-recuperer-une-adresse-email-dans-le-contactbook-de-liphone">Continuer la lecture <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>J’avais dit que je parlerais ici même des petites astuces ou gros problèmes rencontrés lors du développement de mon application iPhone. L’objectif est assez clair : faire gagner quelques précieuses heures aux prochains qui rencontreront les mêmes problèmes que moi. Le premier cas concret concerne le carnet d’adresse de l’iPhone. </p>
<p>L’idée est d’aller récupérer les informations concernant un contact que l’utilisateur aura désigné dans son carnet d’adresse. Je ne vais pas expliquer ici comment mettre en place l’interface de sélection d’un contact (le <code>peoplePickerNavigationController</code>). En effet, Mickaël Morvan le fait très bien dans son <a href="http://mikael-morvan.developpez.com/tutoriels/iphone/sdk/AdressBook/">article sur développez : « <cite>iPhone SDK: Accès au carnet d&#8217;adresses de l&#8217;iPhone</cite> »</a>. </p>
<p>Comme vous avez pu le voir, récupérer le nom et le prénom d’un contact sous forme de chaine de caractère est assez simple. Par contre, si on applique la même technique à l’adresse email (en utilisant <code>kABPersonEmailProperty</code> à la place de  <code>kABPersonLastNameProperty</code>), on lève une exception <code> Terminating_Due_To_Uncaught_Exception</code> … </p>
<p>Pourquoi donc ? La première chose à comprendre c’est qu’un contact n’a qu’un seul nom. Mais il peut avoir plusieurs adresses email… Par conséquent, un <code>ABRecordCopyValue(mPerson,  kABPersonEmailProperty)</code> ne renverra pas une seule valeur mais une collection de valeurs ! Alors, évidement, quand on veut mettre ça dans un <code>NSString</code> ça ne passe pas.</p>
<p>Pour faire simple, on va considérer que seule la première adresse email nous intéresse (généralement c’est la principale). Ce qui revient à vouloir prendre la valeur à l’index 0 de la collection de valeurs. Ensuite, et bien on caste en string. Concrètement, voilà ce que ça donne :</p>

<div class="wp_codebox_msgheader wp_codebox_hide"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p116code34'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11634"><td class="code" id="p116code34"><pre class="objc" style="font-family:monospace;"><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> contactMail <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/"><span style="color: #400080;">NSString</span></a> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ABMultiValueCopyValueAtIndex<span style="color: #002200;">&#40;</span>ABRecordCopyValue<span style="color: #002200;">&#40;</span>mPerson,  kABPersonEmailProperty<span style="color: #002200;">&#41;</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

</p>
<p>Après, il n’est pas dur de compléter en permettant de choisir une adresse email particulière (voir dans la littérature Apple du coté de <code>ABMultiValue</code>). </p>
<p>Mine de rien, le problème a l’air tout simple, mais il a fallu quelques heures pour en arriver là. Sans compter que toutes les recherches faites sont tombées sur des gens posant la question et n’ayant pas de réponse !</p>
<p style="text-align:center;"><a href='http://webd.fr/wp-content/uploads/2008/08/iphone-contact.jpg' rel="lightbox[116]" title="iphone-contact"><img src="http://webd.fr/wp-content/uploads/2008/08/iphone-contact-236x300.jpg" alt="" title="iphone-contact" width="236" height="300" class="alignnone size-medium wp-image-117" /></a></p>
<p><!-- PHP 5.x --></p>
]]></content:encoded>
			<wfw:commentRss>http://webd.fr/116-recuperer-une-adresse-email-dans-le-contactbook-de-liphone/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

