Iphone 在UItableview中按字母顺序对节标题内的联系人进行排序

Iphone 在UItableview中按字母顺序对节标题内的联系人进行排序,iphone,xcode,uitableview,sectionheader,Iphone,Xcode,Uitableview,Sectionheader,我有一个问题,就是按字母顺序将从通讯簿中挑选的联系人排序到部分标题中。我可以按字母顺序排列它们,而不需要章节标题,但是如何根据联系人的姓名来排列它们呢?我不想使用NSDictionary,因为我可以在没有它的情况下进行排序。请查看以下代码:- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [arrayForLastName count]; } -(NSString *)tableVi

我有一个问题,就是按字母顺序将从通讯簿中挑选的联系人排序到部分标题中。我可以按字母顺序排列它们,而不需要章节标题,但是如何根据联系人的姓名来排列它们呢?我不想使用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];
}

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

您可以通过以下方式实现此目的-

1.创建包含所有必需索引的索引数组-

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.对于每个索引,您需要有一个数组来显示这些部分的行。因此,您可以拥有一个包含对应于每个索引的数组的字典


查看本教程,它将帮助您实现此功能-

您可以通过以下方式实现此功能-

1.创建包含所有必需索引的索引数组-

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.对于每个索引,您需要有一个数组来显示这些部分的行。因此,您可以拥有一个包含对应于每个索引的数组的字典


检查本教程,它将帮助您实现此-

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

        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 
{


返回uitable视图中的单元格

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

        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 
{


返回uitable视图中的单元格

rishi我以前读过本教程,但有必要使用字典吗?除此之外还有其他方法吗?如果不使用,则需要为每个字符保留26个不同的数组,这很难处理,也不建议使用。rishi我以前读过本教程ut是否有必要使用字典?除此之外还有其他方法吗?如果不使用此方法,则需要为每个字符维护26个不同的数组,这很难处理,也不建议使用。