Archive

Archive for September, 2009

Object instantied by the interface (XIB) file need to be linked to code

Often using Interface Builder to instantiate some custom classes I use as controllers, I often fell on problems when using these instances. Without giving reason or anything, my program often “bugs”, quits, or anything unexpected when using these instances (for example when pushing a button whose action is set to a method of such an instance).

I imagine this is due to the fact these instances are not quite like ones I could create in my code with a [[CustomClass alloc] init] call, but I do not have scientific explanation! My way of getting out of these problems consists on creating an outlet for these instances in a liable instance. This seems to keep my interface-instantiated object alive!

If you have the scientific explanation to this issue, and the clean way to solve it, I’m listening!

PS: this post could be link to this previous one: My Custom object instantiated in my XIB file is released… is that normal?

Advertisements

Shortest guide to UIScrollView

Want to implement the UIScrollView fast?

  1. Build your scroll view in IB.
  2. Build the content view you wish to scroll.
  3. Link all these things to some class with IBOutlets.
  4. Ask the class (preferably in awakeFromNib) to add your content view as scroll view’s subview.
  5. Last but not least: set your scroll view’s contentSize, or it won’t scroll!
Categories: Cocoa, iPhone SDK Tags: , , , ,

How to add a search bar to your table view, and integrate it in your section indexes?

Assume you have a table view filled with objects. Assume you have sections for these objects, and you have setup the section indexes. Until this point, nothing which cannot be found in SDK’s documentation.

Assume now you want to add a search bar to your table view. What do you do? To answer this question, we will start from source code available to all of us, implementing the required table view with section indexes, we shall use the TableViewSuite’s 3rd example, I have named “3_SimpleIndexedTableView” (you can download the tutorial package there).

From this base, I will indicate the updates necessary to display a search bar in the first row. I will do this the fast way, so it may not be really nice. And I will not implement everything required to do search and display search results, you will have to look at documentation for this (this will essentially consist in connecting the search bar with required objects and delegates, providing content for search results).

So, what do we do to the TableViewSuite’s 3rd example:

  1. We change the numberOfSectionsInTableView: method in order to return one row more (you guess? …the row for the search bar naturally!):
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // The number of sections is the same as the number of titles in the collation.
    return [[collation sectionTitles] count] + 1; // CHANGE: add one for the search cell
    }
  2. We update (a little) the tableView:numberOfRowsInSection: and tableView:titleForHeaderInSection: methods in order to have it return good number of rows for the good sections:
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return 1; // CHANGE: return one for the search cell
    // The number of time zones in the section is the count of the array associated with the section in the sections array.
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:section - 1];
    return [timeZonesInSection count];
    }
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) return nil;
    return [[collation sectionTitles] objectAtIndex:section - 1];

    }
  3. Finally, we update the most important method, I have named tableView:cellForRowAtIndexPath: :
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // CHANGE: add the search cell
    if ([indexPath indexAtPosition:0] == 0) {
    static NSString *CellIdentifier = @"SearchCell";
    UITableViewCell *searchBarCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    [searchBarCell addSubview:[[UISearchBar alloc] initWithFrame:searchBarCell.frame]];
    return searchBarCell;
    }

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    // Get the time zone from the array associated with the section index in the sections array.
    NSArray *timeZonesInSection = [sectionsArray objectAtIndex:indexPath.section - 1];
    // Configure the cell with the time zone's name.
    TimeZoneWrapper *timeZone = [timeZonesInSection objectAtIndex:indexPath.row];
    cell.textLabel.text = timeZone.localeName;
    return cell;
    }
  4. Like I told before, you should then get the searchBarCell connected to the good objects to have it up and working. One last tip, I use the UISearchDisplayController class and connect them both in this way:
    UISearchDisplayController *sdc = [[UISearchDisplayController alloc] initWithSearchBar:sb contentsController:self];
    [sdc setValue:self forKey:@"searchResultsDataSource"];
    [sdc setValue:self forKey:@"searchResultsDelegate"];

Uh oh, I almost forgot to give you the best tip! How to have the “search” tool icon (what’s this tool’s name in English?) in your section indexes? Just follow the guide… (’cause this cannot be found in the doc, but you may find it elsewhere on the net, like I did – however it’s too long ago, I have not kept the source link).

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[NSArray arrayWithObject:@"{search}"] arrayByAddingObjectsFromArray:[collation sectionIndexTitles]];
}

If you have a problem, you can’t make it work in your code, do not hesitate to ask… I have tried this code changes and it worked for me, but I may have forget something!

[Edited] As requested, please find the xCode project there.

%d bloggers like this: