<?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>devedup.com &#187; objective-c</title>
	<atom:link href="http://blog.devedup.com/index.php/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.devedup.com</link>
	<description></description>
	<lastBuildDate>Sun, 09 Oct 2011 16:00:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>UITableView Alternate Row Colours with NSFetchedResultsController</title>
		<link>http://blog.devedup.com/index.php/2011/10/09/uitableview-alternate-row-colours-with-nsfetchedresultscontroller/</link>
		<comments>http://blog.devedup.com/index.php/2011/10/09/uitableview-alternate-row-colours-with-nsfetchedresultscontroller/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 15:57:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ios]]></category>
		<category><![CDATA[UITableView]]></category>

		<guid isPermaLink="false">http://blog.devedup.com/?p=292</guid>
		<description><![CDATA[Ok, this might seem like a simple thing to do &#8211; and in most cases it is, unless you&#8217;re using core data and have some row additions coming in dynamically. - (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColour = (indexPath.row % 2) ? [Constants darkTableCellBackgroundColor] : [Constants lightTableCellBackgroundColor]; } You see the problem [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, this might seem like a simple thing to do &#8211; and in most cases it is, unless you&#8217;re using core data and have some row additions coming in dynamically.</p>
<pre class="brush: objc;">
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColour = (indexPath.row % 2) ? [Constants darkTableCellBackgroundColor] : [Constants lightTableCellBackgroundColor];
}
</pre>
<p>You see the problem I had was, that row zero is white, row one is blue, row two is white&#8230;.etc, and when I synced with my remote server and new data arrived, it was inserted at row zero  - which is white! The other rows were pushed down, so row zero and row one were both white! No more alternate. I could have just refreshed the UITableView &#8211; or the visible rows, but I didn&#8217;t want to do that! I wanted it to figure out the correct row colour from the insert.</p>
<p>I got it working by combining the row index with the total rows, using the inverse of one and XOR&#8217;ing the result! Sounds complicated? Look at this table:</p>
<table border="1" cellspacing="0" cellpadding="2">
<thead>
<tr>
<td>A: Row</td>
<td>B: Row Modulus (%2)</td>
<td>C: Row Count</td>
<td>D: Count Modulus (%2)</td>
<td>E: Inverse Count Modulus</td>
<td>F: XOR B ^ E</td>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>0</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>0</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>4</td>
<td>0</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>As can be seen above, I can get alternate row colours from Column B or Column F. However, if you then add another row at index zero &#8211; the row modulus can no longer be used, but Column F, still preserves the alternate row colours!</p>
<table border="1" cellspacing="0" cellpadding="2">
<thead>
<tr>
<td>A: Row</td>
<td>B: Row Modulus (%2)</td>
<td>C: Row Count</td>
<td>D: Count Modulus (%2)</td>
<td>E: Inverse Count Modulus</td>
<td>F: XOR B ^ E</td>
</tr>
</thead>
<tbody>
<tr style="background-color: yellow;">
<td>0</td>
<td>0</td>
<td>7</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td>0</td>
<td>0</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>2</td>
<td>0</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>1</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<td>4</td>
<td>0</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>5</td>
<td>1</td>
<td>6</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>This can be achieved with this bit of code:</p>
<pre class="brush: objc;">
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    int rowFlag = (indexPath.row % 2);
    int totalFlag = [self tableView:tableView numberOfRowsInSection:indexPath.section] % 2;
    cell.backgroundColor = (rowFlag ^ !totalFlag) ? [Constants darkTableCellBackroundColor] : [Constants lightTableCellBackroundColor];
}
</pre>
<p>Let me know if I&#8217;ve missed something obvious here and there was a more common way of doing this.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.devedup.com/index.php/2011/10/09/uitableview-alternate-row-colours-with-nsfetchedresultscontroller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone: This class is not key value coding-compliant for the key &#8230;..</title>
		<link>http://blog.devedup.com/index.php/2009/11/16/this-class-is-not-key-value-coding-compliant-for-the-key/</link>
		<comments>http://blog.devedup.com/index.php/2009/11/16/this-class-is-not-key-value-coding-compliant-for-the-key/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 21:34:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[key value coding-compliant]]></category>

		<guid isPermaLink="false">http://blog.devedup.com/?p=153</guid>
		<description><![CDATA[Doing some iPhone development and this error really caught me out for a while: 2009-11-16 21:02:48.387 Pickers[3779:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[&#60;UIViewController 0x381d740&#62; setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key datePicker.' 2009-11-16 21:02:48.388 Pickers[3779:207] Stack: ( It&#8217;s seemed fairly cryptic at the time. I had created [...]]]></description>
			<content:encoded><![CDATA[<p>Doing some iPhone development and this error really caught me out for a while:</p>
<pre class="brush: objc;">2009-11-16 21:02:48.387 Pickers[3779:207] *** Terminating app due to uncaught
exception 'NSUnknownKeyException', reason: '[&lt;UIViewController 0x381d740&gt;
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key datePicker.'
2009-11-16 21:02:48.388 Pickers[3779:207] Stack: (</pre>
<p>It&#8217;s seemed fairly cryptic at the time. I had created a Tab Bar Controller and for each tab button, i hadn&#8217;t set its class identity &#8211; so each tab was still declared as a UIViewController instead of the actual Controller class that i had created. </p>
<p>The error comes from the NSKeyValueCoding category which allows you to access properties via setValue: forKey:</p>
<pre class="brush: objc;">
Person *aPerson = [[Person alloc] initWithAge: 53];
aPerson.name = @&quot;Steve&quot;;
// NOTE: dot notation, uses synthesized setter, equivalent to [aPerson setName: @&quot;Steve&quot;];
NSLog(@&quot;Access by message (%@), dot notation(%@),
    property name(%@) and direct instance variable access (%@)&quot;,
      [aPerson name], aPerson.name, [aPerson valueForKey:@&quot;name&quot;], aPerson-&gt;name);
</pre>
<p>So i guess it is just complaining that it can&#8217;t find the generated setter method named setDatePicker &#8211; because datePicker instance is  not  on the generic class.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.devedup.com/index.php/2009/11/16/this-class-is-not-key-value-coding-compliant-for-the-key/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

