Some Cocoa Touch Tips, Not Tricks

I’ve made significant progress on my prototype iPhone app since my last post. I’m continuing to learn something new during each coding session. This post contains my learning nuggets and a few code snippets that I hope you’ll find useful.

Table View Development

I’m developing a productivity app, so it makes heavy use of UIKit’s UITableView classes. Here’s a few key things to remember as you develop your UITableViewController.

    1. Set a table cell’s text elements from your model in thecellForRowAtIndexPath method
    2. Set a cell’s row height by implementing the heightForRowAtIndexPath method
    3. Change the appearance of a cell’s label text (textLabel and detailedTextLabel) by setting the corresponding property.

cell.textLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
cell.textLabel.textColor = [UIColor grayColor];
  1. Respond to row selections in the didSelectRowAtIndexPath method. The code for inclusive and exclusive selection is in Apple’s iPhone Table View Programming guide here.

Manipulating Dates

Displaying dates requires the use of the NSDateFormatter class.

  1. Avoid learning the hard way, case matters in your date format strings, e.g., yyyy-MM-dd. Here’s a good post that covers the problem.
  2. Computing dates (or what Apple refers to as calendrical calculations) is a bit too verbose from me. Here’s how to calculate the top of the hour.

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *minutesComponent = [gregorian components:NSMinuteCalendarUnit fromDate:today];
NSDateComponents *secondsComponent = [gregorian components:NSSecondCalendarUnit fromDate:today];

NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setMinute:0 - [minutesComponent minute] ];
[componentsToSubtract setSecond:0 - [secondsComponent second] ];

NSDate *topOftheHour = [gregorian dateByAddingComponents:componentsToSubtract toDate:today options:0];

Internet Access

For applications accessing internet resources, here’s a few tidbits:

  • To indicate network activity, it’s easy to add spinner or UIActivityIndicatorView to a view. However, if the main thread is too busy, it will not appear.
  • The result of a URL connection (or NSURLConnection) is a binary NSData object, convert it to a string by invoking initWithData in NSString.
  • Parsing JSON objects is a breeze with the JSON Framework for Objective-C. By the way, for server-side Java, check out Flexjson, works like a charm.

Parsing Newlines

Is parsing newlines as simple as searching for “\n”? No, take a gander at this code from Apple’s documentation:

- (NSMutableArray *) parseRawString: (NSString *) rawString  {
	int unsigned length = [rawString length];
	int unsigned paraStart = 0, paraEnd = 0, contentsEnd = 0;
	NSMutableArray *array = [NSMutableArray array];
	NSRange currentRange;
	while (paraEnd < length) {
	      [rawString getParagraphStart:&paraStart end:&paraEnd contentsEnd:&contentsEnd forRange:NSMakeRange(paraEnd, 0)];
	      currentRange = NSMakeRange(paraStart, contentsEnd - paraStart);
	      [array addObject:[rawString substringWithRange:currentRange]];
	return array;

Lastly, Google search and stackoverflow are your friends. If you get stuck, Google it, you’ll most likely find the answer in stackoverflow or the iPhone Dev SDK forum. (However, always read Apple’s iPhone programming guides first).

Well,that’s all I have for now, happy programming!

Some Cocoa Touch Tips, Not Tricks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s