Recent Updates Toggle Comment Threads | Keyboard Shortcuts

  • versluis 4:07 pm on August 28, 2015 Permalink | Reply
    Tags:   

    How to tint the sections in your UITableVIew 

    TableSectionsTo make those sections in a UITableView stand out a bit, you can give them a different colour tint.

    Add a new method in your table view delegate and paint with numbers:

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        
        UIColor *pinkTint = [UIColor colorWithRed:0.7 green:0.5 blue:0.8 alpha:0.7];
        view.tintColor = pinkTint;
    }
    

    The above will create colours in RGB, and values between 0.0 and 1.0 are allowed.

    Or, if you’d rather show a picture instead (as a pattern or because you have a nice colour swatch), grab it from your project and turn it into a colour like so:

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        
        UIColor *pinkTint = [UIColor colorWithRed:0.7 green:0.5 blue:0.8 alpha:0.7];
        view.tintColor = pinkTint;
    }
    

    – (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

    UIColor *niceColour = [UIColor colorWithPatternImage:[UIImage imageNamed:@”NiceColour”]];
    view.tintColor = niceColour;
    }





     
  • versluis 3:50 pm on August 28, 2015 Permalink | Reply
    Tags: , ,   

    How to implement Table Headers with NSFetchedResultsController in Core Data 

    iOS Simulator Screen Shot 28 Aug 2015 15.48.03To use sections in a table view that’s powered by Core Data and a fetched results controller, we need to specify a property called sectionNameKeyPath. It’s the key to making those header titles show up. Reading Apple’s ever so colourful documentation however doesn’t give us a clue what this means in human terms.

    Here’s how to implement those with a simple example.

    Starting out from the Master/Detail template in Xcode, create a new project and choose Core Data. This will setup the fetched results controller along with the rest of the Core Data stack. We’ll also get one handy Event entity with a single attribute: an NSDate called timeStamp. Run the app, click the plus button at the top and we’ll see a new date is inserted into the table.

    But the table has no sections. Let’s assume we’d like to create a new section for every minute that has been logged in the database. So 18:07 would have a section, 18:08, and whatever else is available.

     

    Creating Managed Object Subclasses

    First we’ll create a managed object subclass from our model so that we can write some code for this. Choose the .xcdatamodeld file in Xcode and select Editor – Create NSManagedObject Subclass. Stash the two resulting files in your project folder.

    In Even.h, add another property. It won’t be saved, it’ll be calculated on the fly. We already have the timeStamp property for the date object, so let’s call the new property minuteSection. Here’s the Even.h file:

    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
    
    
    @interface Event : NSManagedObject
    
    @property (nonatomic, retain) NSDate * timeStamp;
    @property (nonatomic, retain) NSString *minuteSection;
    
    @end
    

    In the Event.m file we’ll create a custom initialiser, which will turn our timeStamp into time string. Since this is a Core Data property we’ll have to mark it as dynamic, much like all the other Core Data properties in managed object subclasses:

    #import "Event.h"
    
    
    @implementation Event
    
    @dynamic timeStamp;
    @dynamic minuteSection;
    
    - (NSString *)minuteSection {
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.timeStyle = NSDateFormatterShortStyle;
        NSString *sectionTitle = [formatter stringFromDate:self.timeStamp];
        
        return sectionTitle;
    }
    
    @end
    

    Here we use an NSDateFormatter that turns the long date object into something like 18:07. Every event in our database logged at 18:07 for example will get its own section with that title.

     

    Tweaking the Fetched Results Controller

    Next, let’s turn our attention to the NSFetchedResultsController. In the Xcode template it’s in MasterViewController.m. There’s a custom initialiser with a method that by default looks like this:

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
                                                             initWithFetchRequest:fetchRequest
                                                             managedObjectContext:self.managedObjectContext
                                                             sectionNameKeyPath:nil
                                                             cacheName:@"Master"];
    

    Take a look at the sectionNameKeyPath property here: it’s set to nil, meaning we’re not displaying any sections. To tell the controller which property we want to use as section criteria, all we have to do is provide the string of the property we’d like to use. In our case, that’s minuteSection. Quick modification:

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
                                                             initWithFetchRequest:fetchRequest
                                                             managedObjectContext:self.managedObjectContext
                                                             sectionNameKeyPath:@"minuteSection"
                                                             cacheName:nil];
    

    I’ve also taken the liberty to remove the cache and set it to nil, we don’t really need it here.

     

    Showing the Section Titles

    Right now the app will run as usual, but we still won’t see any new sections. Even though they are displayed, we don’t see them separated from the cells. What we need to do is add a method to our table view delegate and make those sections show up with the right titles.

    In MasterViewController.m, hunt for a pragma mark called Table View. You’ll find methods to show how many rows and sections there are, both of which are being read from the fetched results controller. Examine the code to spot some similarities to what we’re doing next.

    Add a method called titleForHeaderInSection, it comes up as part of the automatic code completion as soon as you type “- table” (it’s a long list). Here’s what we need to add to it for those section headers to show up:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        
        id  sectionInfo = [self.fetchedResultsController.sections objectAtIndex:section];
        return [sectionInfo name];
    }
    

    It’s only two lines of code, but they’re rather long. Here we grab the controller’s current section and call it’s name method, which returns the string we’re looking for (i.e. 18:07, the hour and minute we’d like to use).

    Run the app and add a few values to it. On ever new minute, a new section is created. When you remove values, empty sections are automatically removed as well.

     

    Further Reading

     

    Demo Project

    I’ve got a working demo project on GitHub – feel free to check it out:

     

     

    iOS Simulator Screen Shot 28 Aug 2015 15.48.03





     
  • versluis 11:20 am on June 11, 2015 Permalink | Reply
    Tags: , ,   

    FAIL: iOS 9 can’t restore iCloud Backups from iOS 8 

    iOS9-Sucks

    I thought I’d give the new iOS 9 Beta 1 a spin on my ageing iPad 3. I’ve hit a few stumbling blocks and thought I’d tell you about them.

    The first attempt to add the new beta using iTunes didn’t work and turned my poor iPad into a coaster. Very disappointing. Thankfully a DFU restore did the trick, and iOS 9 was starting fine. Things were looking up.

    I thought it would make sense to restore the latest iCloud Backup I had made just minutes before, thinking that testing with existing data would make sense. But restoring my iCloud Backup didn’t work: I picked one, the restore started, counted down the minutes, and shortly before it was due to finish, it comes up with the message in the screenshot above. Nothing doing.

    I had three backups in total from various dates to choose from. I tried them all, multiple times, but to no avail: none of them could be restored. All I could do was to setup my device as a “new iPad” (after all, that was its launch title). It did make me wonder if all my iCloud Backups were broken or if it was just this particular iOS 9 Beta.

    So I went on to explore a bit, being particularly intrigued by the Slide Over feature: apparently you slide in from the right hand side with one finger and bring up a second app. Doesn’t work of course. Turns out none of these new features work on my iPad 3, nor on the iPad 2: all you can do is use two fingers on the keyboard and move the cursor (no text selection either).

    It was my understanding that Slide Over was supported on all devices, and that only the arbitrary split view and picture-in-picture required this week’s hardware. But since there’s no actual documentation on this topic, your guess is as good as mine. I didn’t even look at the News app, mainly because I’ve un-installed Flipbook and Zite many years ago and have never looked back.

    The new Notes features are a nice touch, 4 years late in my opinion, but better late than never. The interface reminds mea little of 53 Paper. It is required to upgrade existing notes for use with iOS 9, much like the upgrade to iCloud Drive. You can choose not to do it, and the app tells you that upgrading means you can no longer exchange notes with older devices.

    Apple want to make absolutely sure nobody is using old devices anymore: this is the another step in making sure there will be no more data exchange with iOS 5 and iOS 6 devices. It’s top priority for them. Please throw that iPhone 3GS and iPod Touch 4 and iPad 1 away once and for all. We should all go to the store and drop $1000 a month or more just to be able to write a shopping list that works on all our devices.

    I had secretly hoped that iOS 9 would bring performance improvements. My device has been severely crippled with the arrival of iOS 8, and rumour has it that iOS 9 might remedy that. It’s too early to tell I must say: while the keyboard now reflects capital and non-capital letters depending on the shift key, it does take nearly a second for the font to change when you press said shift key or the symbol key. Nothing much has changed there. This could be because betas usually aren’t as fluid as release versions.

    But because I no longer expect anything “nice” from the Cupertino Fruit Co. of late, I wasn’t even disappointed. iOS 9 looks and feels exactly like iOS 8 on my current device, and for me there is no benefit in having it on my device: I don’t intend to roll out any new apps in the near future, so I went back to exploring the iCloud Backup situation.

    I’ve downloaded the iOS 8.3 release version, quickly wiped my iPad and tried to restore my backup again. No problem at all. Phew! Relieved that my backups were still intact, I try my best to forget this plethora of disappointment with iOS 9.

    It’s a bit like how Apple must feel when they’re trying to forget that last year’s OS (or hardware) was ever released.





     
  • versluis 11:25 am on June 9, 2015 Permalink | Reply
    Tags: El Capitan, , ,   

    Apple changes Developer Program 

    Screen Shot 2015-06-09 at 11.18.20With the introduction of watchOS yesterday at WWDC2015, Apple have made some changes to their developer programme: it is no longer required to enrol for membership in the iOS and Mac developer programmes for $99 each. Now there’s only one membership, called Apple Developer for $99 per year. How exciting!

    Apple were nice enough to add up the remaining days in all your enrolments and extend your membership until such time. From then on, only $99 per year are applicable, and not 2x $99 as previously.

    The new Apple Developer membership also includes the previously free Safari Extension programme, as well as new resources for Swift 2 and watchOS.

    All that’s required to go ahead is to sign a new agreement and save some cash. In my opinion, this is seriously good news. I was contemplating of letting my Mac Dev Membership lapse – now I don’t have to worry about that anymore :-)





     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel
Password Reset
Please enter your e-mail address. You will receive a new password via e-mail.