0

I have an NSArray which includes a list of keys and this array comes out of a .plist.

At this moment i write this array in a UITableView, but this is not sorted and sectionized. I want to sort this Array and want to have Sections in this UITableView which begins with the first character of each character in this Array.

As example: Sectionname: "A" Celltext: "Ahorn"

I hope you get it. My Code now:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

NSArray * sections = [temp allValues];

NSUInteger *tablesections = [sections count];

return tablesections;


}

And:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath 

    NSArray * values = [temp allValues];

[EingabeListe addObjectsFromArray:values];

char szDecryptetKey[256];
sleep(0.5);


NSString *cellValue = [values objectAtIndex:indexPath.row];
const char *cString = [cellValue cStringUsingEncoding:NSASCIIStringEncoding];

DecryptKey(cString, szDecryptetKey);

NSString *pnssDecryptetKey = [NSString stringWithFormat:@"%s",szDecryptetKey];


cell.textLabel.font = [UIFont systemFontOfSize:11.0];
cell.textLabel.text = pnssDecryptetKey;

return cell;

Thanks

Ell Neal
  • 6,014
  • 2
  • 29
  • 54
Simon
  • 19
  • 2

4 Answers4

1

I probably would not leave this in a single array. I would put it into a NSDictionary where each letter of the alphabet is a bucket to for each first letter of the alphabet (and a section). Then getting the contents of a single section would be as simple as looking up the first letter you want in the dictionary.

Start by sorting your array alphabetically. This has been asked a lot of times, but here's one answer

Next, iterate over the array and add it to a dictionary based on the first letter. Each "value" in dictionary would be an array, not just a single item. So the first time you'd get to a letter (say 'g') you'd create the "g" key in the dictionary and add an NSMutable array as the value.

As a side note, I didn't add code because this sounded like a homework assignment(of course I could be wrong). While I want to help, I wouldn't want to do it for you. That said, if it's unclear or you want more help, I'd be happy to provide).

Community
  • 1
  • 1
DBD
  • 23,075
  • 12
  • 60
  • 84
0

For that you need to do modification in code; I will explain.

Steps:

  1. Initialize the alphabets array and filter use source array based on the alphabets.
  2. Now dataSource dictionary contains array of source data filtered by alphabets.
  3. Now number of sections will the no. of array in the dictionary.
  4. Load the data source array for each section from the datasource dictionary.

Initialize alphabets array and datasource array:

  alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",
                                    @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
                        
  dataSource = [[NSMutableDictionary alloc]initWithCapacity:[alphabet count]];
                    
  sourceArray = [NSArray arrayWithObjects:@"Azz",@"ax",@"aje",@"B",@"C",@"Ca",@"D",@"DD",@"E",@"EE",@"F",@"G",@"F", nil];
   

Filter the source array and add the data into the dictionary with key values as alphabets:

for(int i = 0; i<[alphabet count]; i ++)
 {
 NSArray *filteredArray = [sourceArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", [alphabet objectAtIndex:i]]];
  if([filteredArray count]>0)
   dataSource setObject:[filteredArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] forKey:[alphabet objectAtIndex:i]]; // Dictionary containing sorted array of data with key as alphabets
                    
}

And you need to customize the number of sections and rows delegate methods. See the sample code:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return [[dataSource allKeys] count];
}
 
      - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section 
{
                return [[dataSource allKeys] objectAtIndex:section]; 
}

Entire source code:

     - (void)viewDidLoad
        {
            [super viewDidLoad];
         
            
            alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",
                        @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil];
            
            dataSource = [[NSMutableDictionary alloc]initWithCapacity:[alphabet count]];
            
            sourceArray = [NSArray arrayWithObjects:@"Azz",@"ax",@"aje",@"B",@"C",@"Ca",@"D",@"DD",@"E",@"EE",@"F",@"G",@"F", nil];
            
            for(int i = 0; i<[alphabet count]; i ++)
            {
                NSArray *filteredArray = [sourceArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", [alphabet objectAtIndex:i]]];
                if([filteredArray count]>0)
                    [dataSource setObject:[filteredArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] forKey:[alphabet objectAtIndex:i]]; // Dictionary containing sorted array of data with key as alphabets
                
            }
            NSLog(@"Filtered Array %@", dataSource);
        
            
        }

        
        #pragma mark - Table view data source
        
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    // Return the number of sections.
    return [[dataSource allKeys] count];
}
 
      - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section 
{
                return [[dataSource allKeys] objectAtIndex:section]; 
}

        
        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            
        
            return [[dataSource objectForKey:[[dataSource allKeys] objectAtIndex:section]] count];
            
        }
        
        - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
        {
            
            return 20;
        }
        - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
        {
            return NO;
        }
        
        
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            
            originalSource = [dataSource objectForKey:[[dataSource allKeys] objectAtIndex:indexPath.section]];
            
            static NSString *CellIdentifier = @"Cell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            cell.textLabel.text = [NSString stringWithFormat:@"%@",[originalSource objectAtIndex:indexPath.row]];
            return cell;
            
        }
        
        
        #pragma mark - Table view delegate
        
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
        
        }
        
        

Output will be like this:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Ganapathy
  • 4,594
  • 5
  • 23
  • 41
0

I usually use the free Sensible TableView framework for these kind of apps. You literally just throw the array to the framework and it will automatically sort and create all the sections for you. Should take you a few minutes to implement so I recommend checking it out.

Matt
  • 2,391
  • 2
  • 17
  • 18
0

Hi thanks it works pretty fine. But i can only see the first Character of my .plist.

I think the wrong line is this one:

NSString *cellValue = [values objectAtIndex:indexPath.row];

But here is my Code:

[super viewDidLoad];
self.EingabeListe = [NSMutableArray arrayWithCapacity:20];
NSIndexPath *indexPath = 0;


alphabet = [[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",
            @"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",@"123",nil];

datasource = [[NSMutableDictionary alloc]initWithCapacity:[alphabet count]];

int m = 1;
int n = 0;
NSArray * values = [temp allValues];
int c = [values count];

//

char szDecryptetKey[256];
sleep(0.5);


while (m != 0) {
    if ( n == c){
        m = 0;
    }else{
        NSString *cellValue = [values objectAtIndex:indexPath.row];
        const char *cString = [cellValue cStringUsingEncoding:NSASCIIStringEncoding];



        NSString *pnssDecryptetKey = [NSString stringWithFormat:@"%s",szDecryptetKey];

        [EingabeListe addObject:pnssDecryptetKey];
        pnssDecryptetKey = 0;
    }
    n++;

}

for(int i = 0; i<[alphabet count]; i ++)
{
    NSArray *filteredArray = [EingabeListe filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF BEGINSWITH[C] %@", [alphabet objectAtIndex:i]]];
    if([filteredArray count]>0)
        [datasource setObject:[filteredArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] forKey:[alphabet objectAtIndex:i]]; // Dictionary containing sorted array of data with key as alphabets

}
}

In my .plist are some Test Keys and Values like this:

Value Key sqq hi zzz eg egg bb

but in my.plist i can only see: sqq sqq sqq

why?

Simon
  • 19
  • 2