Use enum to keep track of UITableView sections

I found an awesome tip at NSScreencast this week. Watch the video “Class Introspection” here.

Define an enum like so:

enum {
    SomeViewControllerFeaturedTableViewSection = 0,
    SomeViewControllerMainTableViewSection,
    ...
    SomeViewControllerTableViewSectionCount
}

Ben from NSScreencast points out that the last value becomes a natural section count. So you can easily do:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return SomeViewControllerTableViewSectionCount;
}

And in the other delegate methods, something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == SomeViewControllerFeaturedTableViewSection) {
        // make and configure a "featured" cell
        return cell;
    }

    if (indexPath.section == SomeViewControllerMainTableViewSection) {
        // make and configure a "main" cell
        return cell;
    }

    ...
}

Now you can simply move around the enum values to temporarily enable or disable sections:

enum {
    SomeViewControllerFeaturedTableViewSection = 0,
    SomeViewControllerTableViewSectionCount, // 1
    SomeViewControllerMainTableViewSection, // 2 (because the count is 1, this will never appear)
}

I really like this trick and plan on using it often. Thanks again to NSScreencast for the idea.

Tagged , , , ,

How to rename an Xcode 4 project and the root source folder

Renaming an Xcode project is not overly difficult, but it does require a few steps. I have been successful with this method:

  • In the Navigator, click on the top-level project element.
  • Hover over the project’s name with the mouse and press enter.
  • In the text field, rename the project and accept Xcode’s prompts.
  • Close Xcode.
  • Rename the source folder.
  • Right-click on the ‘.xcodeproj’ file and “Show Package Contents”.
  • Open the ‘.pbxproj’ file in a text editor and replace all instances of the old project name with the new one.

If everything was done properly, the project can be opened again like normal.

Tagged ,

Stanford’s definition of strong vs weak

This was useful to me, so I am making note of it. From Stanford’s iOS course on iTunes U.

strong vs weak

  • strong “keep this in the heap until I don’t point to it anymore”
    • I won’t point to it anymore if I set my pointer to it to nil.
    • Or if I myself am removed from the heap because no one strongly points to me!
  • weak “keep this as long as someone else points to it strongly”
    • If it gets thrown out of the heap, set my pointer to it to nil automatically (if user on IOS 5 only).

Note to self: option double click in xcode will bring up the full help view

When hovering over a method or a class or… really anything in Xcode, option-clicking will bring up a quick help menu. This is useful, but a lot of times I would rather read the full documentation. That’s where option-double-clicking comes in. It opens the full documentation and jumps right to the item in question.

Tagged ,

Note to self: an object’s getter is one of the best places to initialize it for the first time

Stanford’s free iOS course is a great tool. The video I watched today recommended lazy instantiation of objects.

Example:

- (NSMutableArray *)values
{
    if (_values == nil) _values = [[NSMutableArray alloc] init];
    return _values;
}

Tagged

A blog

Well… I decided to start blogging again. This website will be the home of any content I decide to create for the foreseeable future. I better get busy.

Tagged , ,
Follow

Get every new post delivered to your Inbox.