<?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>Blog d&#039;un développeur multi-support &#187; cell</title>
	<atom:link href="http://www.oni-ecchi.info/blog/tag/cell/feed" rel="self" type="application/rss+xml" />
	<link>http://www.oni-ecchi.info/blog</link>
	<description>[DIM] pour les intimes :)</description>
	<lastBuildDate>Mon, 06 Feb 2012 18:51:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>iPhone &amp; TableView</title>
		<link>http://www.oni-ecchi.info/blog/iphone/iphone-tableview.html</link>
		<comments>http://www.oni-ecchi.info/blog/iphone/iphone-tableview.html#comments</comments>
		<pubDate>Fri, 06 Mar 2009 09:01:02 +0000</pubDate>
		<dc:creator>Guillaume chave</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[cell]]></category>
		<category><![CDATA[cellule]]></category>
		<category><![CDATA[personnalisation]]></category>
		<category><![CDATA[tableview]]></category>

		<guid isPermaLink="false">http://www.oni-ecchi.info/blog/?p=259</guid>
		<description><![CDATA[Coupé depuis 1 mois d&#8217;internet à domicile, je n&#8217;ai pas trop le temps de blogger au boulot&#8230; Ne faisans pas trop de veille technologique et ne pouvant parler des projets d&#8217;entreprises je n&#8217;ai pas grand chose à raconter sous forme de long tutoriel. J&#8217;entame donc une nouvelle façon de blogger mes découvertes : à travers [...]]]></description>
			<content:encoded><![CDATA[<p>Coupé depuis 1 mois d&#8217;internet à domicile, je n&#8217;ai pas trop le temps de blogger au boulot&#8230; Ne faisans pas trop de veille technologique et ne pouvant parler des projets d&#8217;entreprises je n&#8217;ai pas grand chose à raconter sous forme de long tutoriel.</p>
<p style="text-align: justify;">J&#8217;entame donc une nouvelle façon de blogger mes découvertes : à travers de brêves. Aujourd&#8217;hui, pour renouer avec le lectorat &laquo;&nbsp;<a href="http://www.oni-ecchi.info/blog/tag/iphone" class="st_tag internal_tag" rel="tag" title="Voir les articles classés avec iPhone">iphone</a>&nbsp;&raquo;, il sera question de TableView !</p>
<p style="text-align: justify;"><a rel="attachment wp-att-263" href="http://www.oni-ecchi.info/blog/iphone/iphone-tableview.html/attachment/image-131"><img class="size-full wp-image-263 aligncenter" title="TableView" src="http://www.oni-ecchi.info/blog/wp-content/uploads/2009/03/image-131.png" alt="TableView" width="172" height="133" /></a></p>
<p style="text-align: justify;">Avez vous déjà essayé de rendre une ligne sur deux d&#8217;une couleur différente (le background, les textes des labels et leurs états &laquo;&nbsp;survolés&nbsp;&raquo; ? Hey bien c&#8217;est la merde ! Voici la solution que j&#8217;ai pu trouver ici et là.</p>
<ul>
<li>Avoir une classe qui gère une cellule.</li>
<li>Avoir une méthode de colorisation efficace pour la cellule</li>
<li>Détecter si la cellule courante est pair ou impair.</li>
</ul>
<p><span id="more-259"></span></p>
<p>Tout d&#8217;abord, dans la méthode à trifouiller</p>

<div class="wp_codebox_msgheader"><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('p259code6'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2596"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p259code6"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>UITableViewCell <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>tableView<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>UITableView <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSIndexPath <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>indexPath<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Nous devons détecter si la cellule courante est pair ou impair. Ne faites pas comme moi et  ne partez pas de l&#8217;indexPath pour déterminer l&#8217;index courant dans votre tableau d&#8217;élément pour savoir si celui ci est pair ou impair. Cette méthode marche mais elle provoque des bugs d&#8217;affichages sur un iphone (les couleurs se mélangent, s&#8217;alternent etc quand vous scroller trop vite ou d&#8217;une certaine manière). Voici la méthode magique, simple :</p>
<p>
<div class="wp_codebox_msgheader"><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('p259code7'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2597"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p259code7"><pre class="c" style="font-family:monospace;">bool detectPairImpair <span style="color: #339933;">=</span> indexPath.<span style="color: #202020;">row</span> <span style="color: #339933;">%</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Et à coup sur vous aurez le bon résultat. Maintenant la phase de colorisation. Quand j&#8217;initalise ma cellule, je passe en paramètre ce booleen afin d&#8217;avoir la gestion des couleurs dans ma classe cellule. Il suffit d&#8217;appeller la méthode de colorisation adéquate.</p>
<p>
<div class="wp_codebox_msgheader"><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('p259code8'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2598"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p259code8"><pre class="c" style="font-family:monospace;">UIColor <span style="color: #339933;">*</span> greenColor <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UIColor alloc<span style="color: #009900;">&#93;</span> initWithRed<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> green<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span> blue<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> alpha<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
UIColor <span style="color: #339933;">*</span> redColor <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UIColor alloc<span style="color: #009900;">&#93;</span> initWithRed<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span> green<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> blue<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> alpha<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Vous noterez que les couleurs vont de 0 à 1, pensez à diviser par 255 pour passer d&#8217;une représentation RGB classique à ce système. (Je vous apprend la division, pas mal non ? ^^)</p>
<p>Puis, pour le principe, voici ce que j&#8217;utilise. Cette méthode est incomplète et ne marche pas tel quel.</p>
<p>
<div class="wp_codebox_msgheader"><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('p259code9'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2599"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p259code9"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>initWithFrame<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>CGRect<span style="color: #009900;">&#41;</span>frame reuseIdentifier<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>NSString <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>reuseIdentifier pair<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span>detectPairImpair <span style="color: #009900;">&#123;</span>
&nbsp;
UIColor <span style="color: #339933;">*</span> greenColor <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UIColor alloc<span style="color: #009900;">&#93;</span> initWithRed<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> green<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span> blue<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> alpha<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
UIColor <span style="color: #339933;">*</span> redColor <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UIColor alloc<span style="color: #009900;">&#93;</span> initWithRed<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span> green<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> blue<span style="color: #339933;">:</span><span style="color:#800080;">0.0</span> alpha<span style="color: #339933;">:</span><span style="color:#800080;">1.0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>detectPairImpair<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#91;</span>self setCellColor<span style="color: #339933;">:</span>greenColor<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
self.<span style="color: #202020;">libelle</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>self newLabelWithPrimaryColor<span style="color: #339933;">:</span>greenColor selectedColor<span style="color: #339933;">:</span>redColor fontSize<span style="color: #339933;">:</span><span style="color:#800080;">14.0</span> bold<span style="color: #339933;">:</span>YES<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
self.<span style="color: #202020;">libelle</span>.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> @<span style="color: #ff0000;">&quot;Coucou&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>myContentView addSubview<span style="color: #339933;">:</span>self.<span style="color: #202020;">libelle</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
self.<span style="color: #202020;">libelle</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>self newLabelWithPrimaryColor<span style="color: #339933;">:</span>redColor selectedColor<span style="color: #339933;">:</span>greenColor fontSize<span style="color: #339933;">:</span><span style="color:#800080;">14.0</span> bold<span style="color: #339933;">:</span>YES<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
self.<span style="color: #202020;">libelle</span>.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> @<span style="color: #ff0000;">&quot;Salut&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>myContentView addSubview<span style="color: #339933;">:</span>self.<span style="color: #202020;">libelle</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span>self setCellColor<span style="color: #339933;">:</span>redColor<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Je ne rentrerai pas dans les détails pour la création d&#8217;une cellule personnalisé, mais voici les méthodes de colorisations que j&#8217;utilise :</p>
<p>
<div class="wp_codebox_msgheader"><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('p259code10'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25910"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code" id="p259code10"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>UILabel <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>newLabelWithPrimaryColor<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>UIColor <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>primaryColor selectedColor<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>UIColor <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>selectedColor fontSize<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>CGFloat<span style="color: #009900;">&#41;</span>fontSize bold<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>BOOL<span style="color: #009900;">&#41;</span>bold
<span style="color: #009900;">&#123;</span>
<span style="color: #808080; font-style: italic;">/*
Create and configure a label.
*/</span>
UIFont <span style="color: #339933;">*</span>font<span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>bold<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
font <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>UIFont boldSystemFontOfSize<span style="color: #339933;">:</span>fontSize<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
font <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>UIFont systemFontOfSize<span style="color: #339933;">:</span>fontSize<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #808080; font-style: italic;">/*
Views are drawn most efficiently when they are opaque and do not have a clear background, so set these defaults.  To show selection properly, however, the views need to be transparent (so that the selection color shows through).  This is handled in setSelected:animated:.
*/</span>
UILabel <span style="color: #339933;">*</span>newLabel <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UILabel alloc<span style="color: #009900;">&#93;</span> initWithFrame<span style="color: #339933;">:</span>CGRectZero<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
newLabel.<span style="color: #202020;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>UIColor clearColor<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
newLabel.<span style="color: #202020;">opaque</span> <span style="color: #339933;">=</span> YES<span style="color: #339933;">;</span>
newLabel.<span style="color: #202020;">textColor</span> <span style="color: #339933;">=</span> primaryColor<span style="color: #339933;">;</span>
newLabel.<span style="color: #202020;">highlightedTextColor</span> <span style="color: #339933;">=</span> selectedColor<span style="color: #339933;">;</span>
newLabel.<span style="color: #202020;">font</span> <span style="color: #339933;">=</span> font<span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> newLabel<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> setCellColor<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>UIColor<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>color
<span style="color: #009900;">&#123;</span>
cellColor <span style="color: #339933;">=</span> color<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// cellColor est une propriété de la classe de type UIColor</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
this function will layout the subviews for the cell
*/</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>layoutSubviews <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#91;</span>super layoutSubviews<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span> self spreadBackgroundColor<span style="color: #339933;">:</span>self withColor<span style="color: #339933;">:</span>cellColor <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> spreadBackgroundColor<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>UIView<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>that withColor<span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span>UIColor<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>bkColor<span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #009900;">&#123;</span>
NSEnumerator <span style="color: #339933;">*</span>enumerator <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>that.<span style="color: #202020;">subviews</span> objectEnumerator<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
id anObject<span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>anObject <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>enumerator nextObject<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#91;</span>anObject isKindOfClass<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> UIView class<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>UIView<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>anObject<span style="color: #009900;">&#41;</span>.<span style="color: #202020;">backgroundColor</span> <span style="color: #339933;">=</span> bkColor<span style="color: #339933;">;</span>
<span style="color: #009900;">&#91;</span> self spreadBackgroundColor<span style="color: #339933;">:</span>anObject withColor<span style="color: #339933;">:</span>bkColor<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Voilà, j&#8217;espère que ca vous sera utile.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oni-ecchi.info/blog/iphone/iphone-tableview.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

