Question

I have an issue of alphabetically sorting the contacts picked from the address book into section headers . I can arrange them alphabetically without the section headers but how to put them according to the names of the contacts ? I do not want to use the NSDictionary as I am able to do the sorting without it. Please view the code below :-

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [arrayForLastName count];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[alphabets sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [NSArray arrayWithObjects:@"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];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
    return [arrayForLastName count];
}

Any help would be appreciated thanx :)

Was it helpful?

Solution

You can achieve this in following way -

1.Create an index array that contains all the required index -

NSArray *indexArray = [NSArray arrayWithObjects:@"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];

2.For Each index you need to have an array that will display row for those sections. SO you can have a dictionary that contain an array corresponding to each index.

Check this tutorial, it will help you in implementing this- http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/

OTHER TIPS

Try this code in table delegate methode.

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

        return [dataArray1 count];
            }

return number of section in your table view.

       2)        -(NSMutableArray *)selectedarray:(NSString *)countrynmae
         {
        NSString *strQuery=[NSString stringWithFormat:@" select * from Globle where       Country='%@'",countrynmae];
         NSMutableArray *dataarray=[Database executeQuery:strQuery];
        return dataarray;
           }


         - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
           NSDictionary *dict =[dataArray1 objectAtIndex:section];
           NSString *strcountryname=[dict objectForKey:@"Country"];
            NSMutableArray *arr6 = [self selectedarray:strcountryname ];
            return [arr6 count];
           }

Return number of row in particular section

          3)             - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
            {
// create the parent view that will hold header Label
             UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 44.0)];
// create the button object
            UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
           headerLabel.backgroundColor = [UIColor clearColor];
           headerLabel.opaque = NO;
           headerLabel.textColor = [UIColor whiteColor];

            headerLabel.font = [UIFont boldSystemFontOfSize:16];
            headerLabel.frame = CGRectMake(20.0, -14.0, 300.0, 30.0);    
            NSDictionary *dict =[dataArray1 objectAtIndex:section];
             headerLabel.text=[dict objectForKey:@"Country"];
             headerLabel.textColor=[UIColor redColor];

             [customView addSubview:headerLabel];
            return customView;
        }

set header name for section

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

{

       static NSString *CellIdentifier = @"Cell";
             UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
             if (cell == nil)
            {
              cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault     reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.textColor=[UIColor whiteColor];
    cell.backgroundColor=[UIColor clearColor];
            }
             NSDictionary *dict =[dataArray1 objectAtIndex:indexPath.section];
             NSString *countryname = [dict objectForKey:@"Country"];
             NSString *strQuery = [NSString stringWithFormat:@"Select * from Globle where Country = '%@'",countryname];
             NSMutableArray *arr = [Database executeQuery:strQuery];
             NSDictionary *dict1 = [arr objectAtIndex:indexPath.row];
             cell.textLabel.text=[dict1 objectForKey:@"Name"];
             cell.textLabel.numberOfLines=1;
             cell.textLabel.textColor=[UIColor blackColor];
             cell.backgroundColor=[UIColor clearColor];
             cell.textLabel.textAlignment=UITextAlignmentCenter;
               NSString *strImage=[dict1 objectForKey:@"ImageName"];
              UIImageView *imageView1 = [[UIImageView alloc] init];
               UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:
                   [[NSBundle mainBundle] pathForResource:strImage ofType:@"jpg"]];
             imageView1.image = image1;
             imageView1.frame = CGRectMake(6,2,50,42); // position it to the middle
            [cell.contentView addSubview:imageView1];
            return cell;
            [tbl reloadData];
        }

return cell in uitable view

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top