开发者

How to create sectioned/grouped table view alphabetically

I am creating an app that parses huge xml file and gets the data into table view. The user can enter text into search bar to search the table view.

Now, i want to make the t开发者_Go百科able view into a sectioned one that is grouped alphbetically. Sine the table view is huge and the search functionality is included i want to know how to make the table view into sections... Any tutorials or source will be of great help...


You must use a table view configured as an section index
See Table View Programming Guide for iOS

Populating an Indexed List

[...]An indexed list is a table view in the plain style that is specially configured through three UITableViewDataSource methods:

  • sectionIndexTitlesForTableView:
  • tableView:titleForHeaderInSection:
  • tableView:sectionForSectionIndexTitle:atIndex:

The first method returns an array of the strings to use as the index entries (in order), the second method maps these index strings to the titles of the table-view’s sections (they don’t have to be the same), and the third method returns the section index related to the entry the user tapped in the index.

The data that that you use to populate an indexed list should be organized to reflect this indexing model. Specifically, you need to build an array of arrays. Each inner array corresponds to a section in the table; section arrays are sorted (or collated) within the outer array according to the prevailing ordering scheme, which is often an alphabetical scheme (for example, A through Z). Additionally, the items in each section array are sorted. You could build and sort this array of arrays yourself, but fortunately the UILocalizedIndexedCollation class makes the tasks of building and sorting these data structures and providing data to the table view much easier. The class also collates items in the arrays according to the current localization.


You can get the sample project at https://github.com/shreeshgarg/sectiontable

First you have to sort the data using NSSortDescriptor, than create a dictionary which keys are first letter of data each key should have an array of records starting from same letter.

you can do it as

-(NSMutableDictionary *)fillingDictionary:(NSMutableArray *)ary
{

// This method has the real magic of this sample
// ary is the unsorted array
// keyArray should be global as you need to access it outside of this function

     keyArray=[[NSMutableArray alloc]init];
    [keyArray removeAllObjects];

    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];

    // First sort the array

    [ary sortUsingSelector:@selector(compare:)];


    // Get the first character of your string which will be your key

    for(NSString *str in ary)
    {
        char charval=[str characterAtIndex:0];
        NSString *charStr=[NSString stringWithUTF8String:&charval];
        if(![keyArray containsObject:charStr])
        {
            NSMutableArray *charArray=[[NSMutableArray alloc]init];
            [charArray addObject:str];
            [keyArray addObject:charStr];
            [dic setValue:charArray forKey:charStr];
        }
        else
        {
            NSMutableArray *prevArray=(NSMutableArray *)[dic valueForKey:charStr];
            [prevArray addObject:str];
            [dic setValue:prevArray forKey:charStr];

        }

    }
    return dic;

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜