Category Archives: IOS Development

Change the background colour of UITableViewCell

In one of the IOS apps I am currently working on I been using a table view controller to display information for a core data database.  I was wanting to change the background colour of the UITableCell depending on a boolean variable in my manged objects.

Originally I tried setting this in the UITableView delegate method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

After I quick test I realised that because the UITableViewCell is effectively recycled (de-cued and used again) this approach was not going to work.

After a little more reading I found the UITableView delegate method

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

This is what you need to use if you want to change a cell that is displaying a specific row (index path) in your table.

Here is an example of the code I used in my app:

 

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    MonkeyManagedObject *monkey = [self.fetchedResultsController objectAtIndexPath:indexPath];
    
    
    if ([monkey.hasBanana isEqualToNumber:[NSNumber numberWithBool:YES]] ) {
        
        cell.backgroundColor = [UIColor redColor];
        
        
        
    } else {
        
        
        cell.backgroundColor = [UIColor whiteColor];        
    }
    
    
    
    
    
    
}

In my code I am using a NSFetchedResultsController to populate my table view also because the Boolean type in a managed object is actually an NSNumber you need to use the NSNumber class method:

[NSNumber numberWithBool:YES]

View your core data SQLite database

I am currently writing an ios app that uses a medium size pre-populated core data database. Having worked with mysql previously along side php and java I have to say that the way core data is implemented makes it very powerful and great to use for object persistence.
In my app I am opening a plist file as a dictionary and looping through each entry to create the core database within the app.
I am also using a UITableView and a NSFetchedResultsController to display the table in the app which is another strength of core data as it automatically reloads the table when the data changes.
The first time I ran the app and viewed the table some of the objects had the wrong section  headers and also some of the section titles were incorrect.
When I checked the plist files the entires that were showing up incorrectly were fine in the plist file.  In mysql I would have just done a select * to see all the entires in the table to see what the database looked like but wasn’t sure how to do this with core data.

This is what I ended up doing to find the incorrect entities:

I set the SQL debug flag in xcode. In xcode 4 this can be done from the Product menu by selecting “Manage Schemes” and then editing the current scheme you are using. Select the “Run “phase and then select the “Arguments” tab. Here you will see options to set arguments and environment variables. Add the code below as an argument:

-com.apple.CoreData.SQLDebug 1

I then ran my app in the ios simulator which gave me the location of the sql file on my computer. I copied the location (minus the sql file name) and pasted it into the “Go to Folder” field in the “Go” menu of finder then copied the .sql file into my user folder.

I then downloaded SQLite Database Browser which is a free open source SQLite browser (the title is pretty explanatory really) and opened my .sql file and viewed the tables.

I found the entires which were incorrectly entered and fixed them in my .plist file that populates my core data database which in tern fixed the problem with the section headers I was having. All without too many (more) hairs turning grey!

Audio Conform Calculator Launched

Every now and then I am having to time compress or expand audio I am working on to keep it in sync with the video as it is going to be used in or is going to cinema, comes from overseas and been converted from NTSC to PAL or is shot at a non-standard frame rate, such as 22 frames a second for effect.
Using a plug-in such as Serato pitch and time (which is my favorite) in Pro Tools you can maintain the pitch of the audio while changing the duration to match the video.

Audio Conform Calculator is a simple iPhone App in which you can specify the source and destination frame rates of the the video you are conforming and it will provide you with the percentage and ratio value that the corresponding audio needs to be time expanded or compressed to keep it in sync with the video.

You can view more details of the app from the
Audio Conform Calculator page on this site.

I built this app originally as a desktop java application but decided it was much more useful as an iPhone app.
After coding for a couple of years in Java and PHP I have to say that IOS and the tools provided are a real pleasure to work with. Although there is a bit of a learning curve switching to the model view controller programming paradigm and other IOS quirks I am starting to get my head around a lot of it.

Prepare for Segue in IOS 5

I’ve been working on a few iPhone and iPad apps using IOS 5 and have been watching Paul Hegarty’s iPhone course at Stanford though iTunes U. I recommend watching it for anyone who is interested in IOS development as it’s as fantastic source of information and Paul is a great teacher.

One of the many useful things I found when first delving into the model view controller paradigm was the method that is called just before a Segue to another view controller prepareForSegue:

You can use this to set properties in the view controller you are about to segue to such as the delegate which is very useful.
You just need to set the identity of the segue in Xcode so it can be identified.

Here is a small snippet based on Paul’s example

-(void) prepareForSegue:(UIStoryboardPopoverSegue *)segue sender:(id)sender
{
    if ([segue.identifier hasPrefix:@"Next View"]) {
      
        //The segue is to the a View Controller called NextViewController
        (NextViewController *nvc =  (NextViewController  *) segue.destinationViewController;
       
       //Set properties in the view controller - note the views outlets are not set yet
        nvc.stringProperty1 = @"This Is Property 1";
      
         nvc.property2 = @"This Is Property 2";
       //Set self as the delegate
       nvc.delegate = self;
    }
}