List of fonts installed on the iPhone/iPad

I finally got around to updating this for the iPad. The project is setup for iOS 6. One other point I should add:
After converting this to support iPad, I noticed an annoying problem where the content of a given cell would “disappear” when the cell reappeared in the view after scrolling. Undrawn cells below or above the view would appear empty when first scrolled onto the screen. This only happened when the device, iPhone or iPad, was lying flat on a table. I tracked this down to a couple of issues with my cellForRowAtIndexPath method.
First, never do any more inside the if(cell==nil) clause than is needed in every cell. Remember if the cell is reused, anything inside that clause won’t get executed when another cell is requested. In this example. All I do is initialize the cell, set the minimum font size and constraint width of the text, every cell needs those. The text, which varies with each cell, is after the ‘if’ clause. Also, I had to replace initWithFrame with initWithStyle:reuseIdentifier, not sure why I was doing it the other way. The real problem I found is that, for example, when you place an iOS device flat on a table, [[UIDevice currentDevice] orientation] will always return UIDeviceOrientationFaceUp. My code was adding the text without considering this situation, so neither of my if/else clauses was executing, therefore no text. So I just check for landscape orientation and draw the text accordingly, or portrait when face up. That’s it.

Just a quick post to show how you can get a list of the fonts installed on the iPhone. There are lists available on the internet, but I was bored one day and decided to write an iPhone app that displays them all in a grouped table view subdivided by font family. Its based on some sample code I found here. The only problem with that code is that since it outputs the results to the console, you don’t know how the font actually looks. This little project displays each font string using the font itself. There’s also a simple example of how to create and initialize a table header view from a xib. Here’s the project. Sample output is here:
FontViewer output

I hope you find this useful.

missing source managed object model

I’ve been working through the Marcus Zarra book “Core Data” and although it seems quite good, I’ve found a few things that could have been made a bit clearer. As with most any book, there are errors, you can find the errata for the Core Data book here: Core Data errata . But some things are not as clear as they could be for total novices to CD.  As you follow the book through and build the sample projects (available here: Core Data source), you might find things don’t work as the book indicates they should.

I’m going to assume you can figure out the Cocoa related issues, but the CD issues may not be as straightforward. In particular, when running the second version of the second project, you may get the error: “missing source managed object model”. I only found a couple of references to this error when googling, but because there were so few, and I double and triple checked my code against the book, I assumed the problem must be me. As usual, it was. The simple answer is this: Don’t give your project the exact same name as Zarra does in his book, if you want to also run the projects included in the book’s source code. There is a hash generated when a Core Data project is run and it’s stored in the xml file in the Application Support folder. This hash is used when running a CD project and when migrating a CD data store to a new version. If you run Zarra’s code and the project has the same name, the hash will get changed. If you then attempt the second version of the project with this changed hash, you’ll get the title of this post as an error. If you do get the error, delete the xml file, run the first version of the project and recreate the data. Now you should be able to run the second version of the project and your data will be migrated as expected. I’m sure there are more details that I’m not aware of yet, but this should get you passed this error. I’ll continue to post as I work through the book, so feel free to check in from time to time.

Mutually exclusive NSTableView selections

While developing Weather Vane, I ran into a problem trying to bind a selection from one of two different tables to a location ivar, which can only reflect the value of one selection at a time. In Weather Vane’s preferences, when a user does a search for a city, multiple results may be returned, some international and some US. International and US results are placed in separate tables. If the user selects a US city, the ivar is updated as expected. If the user then selects an international city, the ivar is again updated, but if the user then reselects the original US city, the ivar is not updated. The reason had to do with how and when the NSTableViewSelectionDidChangeNotification is sent.

When the initial selection was made in the US table, the notification is sent, the same occurs when a selection is made in the international table. But when the same selection is made again in the US table, the original selection is still valid even though the US table lost focus when the international selection was made, so reselecting it does not trigger an NSTableViewSelectionDidChangeNotification.

When the NSTableViewSelectionDidChangeNotification is sent, an NSTableView delegate method is invoked (if you’ve chosen to be notified of selection changes, or set some class in your project as the NSTableView’s delegate):

– (void)tableViewSelectionDidChange:(NSNotification *)aNotification

My initial attempt to resolve the selection problem tried to handle the problem in this method by deselecting everything in the ‘other’ table (the one losing focus), but what I found is that this led to additional notifications being sent. Every selection triggers a notification AND every deselection triggers a notification as well. One could easily end up with looping notifications this way, which is a problem.

I have a small project which illustrates the problem. You can download it here. (Please note: Although I called the project mutex_tables, one should not confuse this with locks of any sort). The name was determined from the mutually exclusive nature of the tables.

When the tables are initially populated, things look like this:
Initially populated tables

Once a selection is made, an NSTextField below the tables has its stringValue set (like my location ivar).
Initial selection made, NSTextField updated.

After making a selection in the second table, the NSTextField is also updated:
Second selection made, NSTextField updated.

Notice, the selection in the first table, although grayed out since the table has lost focus, is still highlighted.

Now, if we select the same item in the first table, nothing happens. Since technically, there is no change in the selection, the notification is not fired and the text field is not updated.
Original selection made, NSTextField NOT updated..

The solution I found is to use another delegate method,

-(BOOL)tableView:(NSTableView*)aTableView shouldSelectRow:(NSInteger)rowIndex

to handle the deselection in the ‘other’ (unfocused) table.

When called, a selection is about to made in one of the tables. What I do then is deselect everything in the ‘other’ table. Now, in tableViewSelectionDidChange, I can just “find” the new selection and update the textfield accordingly. tableViewSelectionDidChange will be called once on the initial selection, and twice thereafter, but never more than twice. Two notifications seem to be unavoidable.

Here’s how things look now after I implemented the new delegate method:

Initial unselected view:
Initially populated tables

After first selection is made:
Initial selection made, NSTextField updated.

After second selection in the other table is made:
Second selection made, NSTextField updated.

But now with our newly implemented delegate method in place, reselecting the original item in the first table, things work properly:
Reselect initial item in the first table, NSTextField updated.

Nothing in the unfocused table is highlighted and as selections are made between the tables, only the current selection in the current table will be highlighted.

Here is my implementation of tableView:shouldSelectRow:

-(BOOL)tableView:(NSTableView*)aTableView shouldSelectRow:(NSInteger)rowIndex{

// Deselecting generates an NSTableViewSelectionDidChangeNotification
// notification.
if (aTableView == self.secondTableView) {
[self.firstTableView deselectAll:self];
}
else {
[self.secondTableView deselectAll:self];
}
return YES;
}

And my tableViewSelectionDidChange
// This will be called once on the initial selection
// twice thereafter.
– (void)tableViewSelectionDidChange:(NSNotification *)aNotification{

NSDictionary * item = nil;
NSUInteger index;

// Make sure we have the right table and ignore no-selection
if([aNotification object] == self.firstTableView && [self.firstController selectionIndex] != NSNotFound){
index = [self.firstController selectionIndex];
item = [self.firstTableArray objectAtIndex:index];
}
else if([aNotification object] == self.secondTableView && [self.secondController selectionIndex] != NSNotFound){
index = [self.secondController selectionIndex];
item = [self.secondTableArray objectAtIndex:index];
}

// When two notifications are sent, item will be nil on first notification
if (item) {
[self.selectionField setStringValue:[item objectForKey:@”test”]];
}
}

You can download the updated project here.

By the way, don’t forget to check the “Avoid Empty Selection” box for the associated NSArrayController.

I hope people find this useful. If someone has a better way, please feel free to comment.