How to create sectioned/grouped table view alphabetically

前端 未结 2 1047
刺人心
刺人心 2021-01-15 03:31

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 m

2条回答
  •  渐次进展
    2021-01-15 03:36

    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;
    
    }
    

提交回复
热议问题