如何在分组表视图中显示存储为全局数据的xml数据 [英] How to display xml data that is stored as Global data in grouped table view

查看:97
本文介绍了如何在分组表视图中显示存储为全局数据的xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用XML Parser来解析xml文件并存储为全局数据。我正在获取该数据并将其显示为表格。我使用此示例包含搜索功能

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...

赞 - - >> | A |

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; 

任何人都可以告诉我如何在表格视图中显示数据作为部分???任何示例代码或教程都会有很大的帮助...请帮助...

Can anyone please tell me how to display the data in table view as sections??? Any sample code or tutorial will be of great help... Please help...

推荐答案

你没有提到,你是怎么做的数据看起来像你解析它的数据类型。我假设全局数据意味着可以在应用程序范围内访问的一些NSArray。

如果你没有这样做,你应该考虑使用CoreData。

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.

我改编了Apples示例代码 CoreDataBooks ,以便它不使用作者名称来生成部分,但是它的第一个字母。

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

add在RootViewController实现之前的这个类别:

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

在RootViewController的中 - ( NSFetchedResultsController *)fetchedResultsController getter

in RootViewController's - (NSFetchedResultsController *)fetchedResultsController getter

更改

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

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

编辑回复评论

您可以使用NSDirectory,它将第一个字母作为键,将NSArray作为对象保存所有相关数据对象。

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


  • numberOfSectionsInTableView:将返回 [[theDirectory allKeys] count]

  • tableView:titleForHeaderInSection:返回 [[theDirectory allKeys] objectAtIndex :部分] 。这里没有显示排序。

  • tableView:numberOfRowsInSection:返回 [theDirectory objectForKey:[[theDirectory allKeys] objectAtIndex :section]]

  • tableView:cellForRowAtIndexPath:你使用 indexPath .section indexPath.row 来解析正确的数据。

  • 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.

我没有测试过这个。

这篇关于如何在分组表视图中显示存储为全局数据的xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆