开发者

How to display xml data that is stored as Global data in grouped table view

I am using XML Parser to parse a xml file and store as Global data. I am fetching that data and displaying it as a table. I am using this sample to include search functionality.

I wanted to display the dat as grouped table view with alphabets and the titles...

Like --->> | A |

        Adam

        Apple...

Here is how I am displaying the table earlier without groups:

-(UITableViewCell *) tableView : (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath 
{
  开发者_开发百科  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"acell"]; 

    if(cell == nil) 
    { 
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"acell"] autorelease]; 
    }

    if(searching)
    {
        Boat *copyboat = [copyListOfItems objectAtIndex:[indexPath row]];

        cell.textLabel.text = [copyboat boatName];
        NSLog(@"%@", [copyboat boatName]);
    }
    else {
        NSSortDescriptor *alphaDesc = [[NSSortDescriptor alloc] initWithKey:@"boatName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
        [[data boats] sortUsingDescriptors:[NSMutableArray arrayWithObjects:alphaDesc, nil]];   
        Boat *fullboat =[data.boats objectAtIndex:[indexPath row]];
        cell.textLabel.text =[fullboat boatName];
        [alphaDesc release];
    }

    return cell; 
}

How do I display the data in table view as sections?


You are not mentioning, how your data looks like, to what kind of data you parse it. I assume that "Global data" means some NSArray that is accessible app-wide.
You should really consider of using CoreData, if you are not doing so.

I adapted Apples sample code CoreDataBooks so that it is not using the authors name to generate the sections, but its first letter.

add this Category on Book before the implementation of RootViewController:

@interface Book (FirstLetter)
- (NSString *)uppercaseFirstLetterOfName;
@end

@implementation Book (FirstLetter)
- (NSString *)uppercaseFirstLetterOfName {
    [self willAccessValueForKey:@"uppercaseFirstLetterOfName"];
    
    NSString *aString = [[self valueForKey:@"author"] uppercaseString];     
    NSString *stringToReturn = [aString substringToIndex:1];
    
    [self didAccessValueForKey:@"uppercaseFirstLetterOfName"];
    if ([stringToReturn integerValue] || [stringToReturn isEqualToString:@"0"] ) {
        return @"#";
    }
    return stringToReturn ;
}
@end

in RootViewController's - (NSFetchedResultsController *)fetchedResultsController getter

change

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"author" cacheName:@"Root"];

to

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"uppercaseFirstLetterOfName" cacheName:@"Root"];

edit in reply to comment

You could us a NSDirectory, that holds the first letter as key and a NSArray with all related data-objects as object.

  • numberOfSectionsInTableView: would return [[theDirectory allKeys] count]
  • tableView:titleForHeaderInSection: returns [[theDirectory allKeys] objectAtIndex:section]. Sorting not shown here.
  • tableView:numberOfRowsInSection: returns [theDirectory objectForKey:[[theDirectory allKeys] objectAtIndex:section]]
  • and in tableView:cellForRowAtIndexPath: you use indexPath.section and indexPath.row to adress the right data.

I didn't test this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜