Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional iPhone and iPad Database Application Programming
This is the forum to discuss the Wrox book Professional iPhone and iPad Database Application Programming by Patrick Alessi; ISBN: 978-0-470-63617-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional iPhone and iPad Database Application Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old June 28th, 2011, 02:17 AM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Error in chapter 3 - anyone have a fix?

I went back to chapter 3 and, unfortunately, found an error in the code. I know the problem is not on my end because the error can be duplicated in the downloaded code. I was hoping someone might have a fix for this.

To duplicate the problem in the sample code, change the record "Widget A" to read "A Widget" (I did this using the FireFox add-on for SQLite, btw). Now that there is a record in the 'A' section, when you search for *anything* at all, the 'A' section header will stay on the screen, regardless of the results returned. No other headers appear, just the 'A' header.

I think I've narrowed the problem down to RootViewController.m numberOfSectionsInTableView, but none of the fixed I tried worked.

Does anyone have a fix for this? I've looked for help online, but nothing yet has resolved the problem. I'd even be willing to try 'work around' fixes, such as covering over the header with other text (e.g. 'search results')... but I've had no success.

I'd be very grateful for any help.
  #2 (permalink)  
Old June 28th, 2011, 09:32 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The error is in
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

There are 2 tableViews that can be passed to this method, self.tableView which displays everything and UISearchResultsTableview which is passed in when searches are being done and displays in a single section, section 0, all the filtered results. As long as section 0 of the self.products is empty no problems arise since the count of self.products section 0 is also 0. Once section 0 of self.products has elements a problem arises since the count is not zero. The conditional results in the title being displayed since it does not take into account which tableView has been passed in. The titles should only be returned if self.tableView is passed in.
Amend the method as follows:

Code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // Make sure that the section will contain some data

    if (tableView == self.tableView && [[self.products objectAtIndex:section] count] > 0) { 

        // If it does, get the section title from the UILocalizedIndexedCollation object
        return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]
                objectAtIndex:section]; }
    return nil;
}
Bob
The Following User Says Thank You to thepianoguy For This Useful Post:
tecky10 (June 28th, 2011)
  #3 (permalink)  
Old June 28th, 2011, 12:48 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Thank You!! And question...

Thanks, Bob, that solved it! Also, your explanation gave me a better idea of how this works…something super helpful to a newbie like myself.

Could I also ask you if it is possible to do the following with the headers/index? I thought it might be possible to do this, but I've not found anything that leads me to believe it is.

What I'd like to do is have headers like so above the cells (instead of A-Z)…

Apples
Bananas
Carrots
etc…

But still retain the vertical A - Z index on the right (e.g. clicking on the 'A' in the vertical index would take you to the first header that starts with 'A'). Is this possible to do or must the index at the right match the headers?

Thanks again for your truly valuable help and advice.
  #4 (permalink)  
Old June 28th, 2011, 02:59 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Taking the books project as an example. I changed the headers to match what is displayed i.e. Device, Part, Tool.

To do this I added an array to hold the display list

Code:
@interface RootViewController : UITableViewController {
    NSMutableArray *products;
    NSArray *filteredProducts;
	NSArray *displayList;

    UISearchBar* searchBar;
    UISearchDisplayController* searchController;
}

@property (retain, nonatomic) NSMutableArray *products;
@property (retain, nonatomic) NSArray *filteredProducts;
@property (nonatomic, retain) NSArray *displayList;
@property (retain, nonatomic) UISearchBar* searchBar;
@property (retain, nonatomic) UISearchDisplayController* searchController;



@end
with the corresponding
@synthesize displayList;
in the RootViewController.m file

in the -viewDidLoad method, a temporary (setUpDisplayList) array is initialized and then as the products are assigned to their sections the product name is parsed so that the first part can be used in the display (i.e. Device N becomes Device, Part Alpha becomes Part etc). and added to the proper index in the temporary displayList. This is then assigned to the NSArray displayList.

Code:
NSInteger displayListSize = [[indexedCollation sectionTitles] count];
	NSMutableArray *setUpDisplayList = [[NSMutableArray alloc] initWithCapacity:displayListSize];
	for (int i = 0; i < displayListSize; i++) {
		[setUpDisplayList addObject:@""];
	}

    for (Product *theProduct in productsTemp) {
        NSInteger section = [indexedCollation sectionForObject:theProduct 
                                       collationStringSelector:@selector(name)];
		if ([setUpDisplayList objectAtIndex:section] == @"") {
		NSArray *nameSubstring = [theProduct.name componentsSeparatedByString:@" "];
		[setUpDisplayList replaceObjectAtIndex:section withObject:[nameSubstring objectAtIndex:0]];
		}
        theProduct.section = section;		
    }
	self.displayList = setUpDisplayList;
	[setUpDisplayList release];
Then in …titleForHeaderInSection replace the return with

Code:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    // Make sure that the section will contain some data
    if (tableView == self.tableView && [[self.products objectAtIndex:section] count] > 0) { 
        // If it does, get the section title from the UILocalizedIndexedCollation object
		return [self.displayList objectAtIndex:section];}
     //   return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]
     //           objectAtIndex:section]; }
    return nil;
}
I have another post which deals with changing the section headers here
Chapter 7 ViewTaskController - 3 Sections in Table
A slightly different approach, involving setting up conditions for determining the titles for the header NSString, instead of an indexed array.
Bob
The Following 2 Users Say Thank You to thepianoguy For This Useful Post:
IandiPhone (December 16th, 2011), tecky10 (June 28th, 2011)
  #5 (permalink)  
Old June 28th, 2011, 07:28 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Section headers + index at right

Thanks, Bob. I tried the code you indicated and it both inserts items in section headers & allows me to use the A-Z index on the right (yay!). There was one problem, though. It just included a single section header for each letter.

For example, I got results like so...

APPLES (section header)
Granny Smith
Macintosh
Asparagus
BANANA (section header)
Yellow Banana
Red Banana
Baby Banana
Blackberries
Blueberries
Raspberries
...

Instead of...
APPLES (section header)
Granny Smith
Macintosh
ASPARAGUS (section header)
Asparagus
BANANA (section header)
Yellow Banana
Red Banana
Baby Banana
BERRIES (section header)
Blackberries
Blueberries
Raspberries
...

Would there be a limit of one header per letter in order to use the vertical A - Z index on the right?

Thanks again for your help!
  #6 (permalink)  
Old June 29th, 2011, 07:53 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

It would seem to make more sense to me to have detail views instead of multiple headings when you have subcategories. An index of 27 items, as in the project (A-Z plus the #), suggests 27 sections max. Your approach has more headings than indices. When you have multiple items in a category, i.e. your apples, bananas, berries groups, you should push a detail view on, listing the items in the group. The design would then simply return to alphabet headings, with alphabetized entries containing detail views when needed. This "tree" approach seems logical.
I believe I could make a display work the way you describe. I'll see if I can get something together over the next day or so. Basically you would need to track the number of "A", "B", "C" categories, and have the index return to the first in each group, so if you had, as in your example, 2 in group "A", index 0 would call section 0, index 1 would call section 2 etc. I would need to work out the details of the implementation, but conceptually it is not that hard to do. I need to see how the index calls the section. It is one of those "free" functionality things, whose details I have not looked in to.

I'll get back on this, but today is very unlikely.

Bob
The Following User Says Thank You to thepianoguy For This Useful Post:
tecky10 (June 29th, 2011)
  #7 (permalink)  
Old June 29th, 2011, 01:05 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Thank you!

Thanks, Bob. You're great.

I hear what you're saying about the detail views, but reducing the number of clicks is a goal and also the individual categories starting with the same letter aren't necessarily related to each other (e.g. Apples category & Asparagus category are not related). Further, categories may have only one item (e.g. Asparagus category), so users would probably not want to have an additional click to view a single item. For this app, being able to jump quickly to the first header for each letter would be nice, if it was possible.

You are right about having more headings than indices. I'll be amazed if you are nevertheless able to figure out how this can be done. Thanks very much for trying!

BTW, when you look into things like this, are there any sources you find especially helpful besides Apple? For some reason, I usually find Apple documentation the most confusing of all (I think they are the most comprehensive, but the most confusing). Yet, I still need a lot of help, so I'm trying to find other sources that are reliable & easier for a newbie like myself to comprehend. If you are more comfortable recommending resources privately, please feel free to send me an e-mail (you probably have my e-mail address from the Chapter 7 project or I could send it to you privately). I welcome any suggestions you may have.

Thanks much for your help and advice. I look forward to hearing back from you.
  #8 (permalink)  
Old June 30th, 2011, 01:56 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

I have implemented what you wish to do and am emailing the project. All changes/additions have been made to the download code provided by the author. They are noted in the comments, without explanation. Try to sort out what is going on. To keep the changes to a minimum I created categories (the headings for the sections) by simply adding a descriptive word to the beginning of the product name in the database. I then parsed the first word of the Product name and used that for the heading. Obviously, the category could normally be part of the data structure, eliminating the parsing.
The index also works as expected.

I don't believe there are any issues with the implementation, but testing, due to time constraints, has been limited.

Bob
The Following 2 Users Say Thank You to thepianoguy For This Useful Post:
IandiPhone (December 16th, 2011), tecky10 (June 30th, 2011)
  #9 (permalink)  
Old June 30th, 2011, 06:08 PM
Authorized User
 
Join Date: May 2011
Posts: 19
Thanks: 9
Thanked 0 Times in 0 Posts
Default Perfect!

That is exactly what I was looking for! Thank you so much! :-)
  #10 (permalink)  
Old December 16th, 2011, 02:32 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 8
Thanks: 6
Thanked 0 Times in 0 Posts
Default

I have exactly the same problem and the solution would also be correct for me.
Bob, can you show me the solution (or just send an e-mail), please?
I sat on the problem for a long time. It would be a great help.

Thank you very much, Michael
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Fix: Chapter 7 - Earthquake IllegalStateException Finalizing cursor juniper BOOK: Professional Android 2 Application Development 3 August 19th, 2011 03:25 AM
Chapter 4 - CakePHP Form ID Passing fix Akira71 BOOK: Building PHP Applications with Symfony, CakePHP, and Zend Framework 3 April 6th, 2011 07:40 AM
Fix Error sgafar ASP.NET 1.0 and 1.1 Basics 0 May 30th, 2007 10:22 AM
New Chapter 3 Fix .MAttButler BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 0 August 14th, 2006 09:08 PM



All times are GMT -4. The time now is 11:01 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.