0

我有一个按字母顺序将从地址簿中挑选的联系人排序到部分标题的问题。我可以在没有部分标题的情况下按字母顺序排列它们,但是如何根据联系人的姓名放置它们?我不想使用 NSDictionary,因为没有它我也可以进行排序。请查看以下代码:-

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

任何帮助将不胜感激thanx :)

4

2 回答 2

4

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/

于 2012-06-28T08:54:55.367 回答
0

在表委托方法中尝试此代码。

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

        return [dataArray1 count];
            }

返回表格视图中的部分编号。

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

返回特定部分的行数

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

设置节的标题名称

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

在合适的视图中返回单元格

于 2012-06-28T09:39:45.667 回答