xcode 按字母顺序对 UItableview 中的部分标题内的联系人进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11240723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alphabetically sorting the contacts inside a Section header in a UItableview
提问by Tejeshwar
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 :-
我有一个问题,从地址簿中挑选的联系人按字母顺序排列到节标题中。我可以在没有节标题的情况下按字母顺序排列它们,但如何根据联系人的姓名放置它们?我不想使用 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];
}
Any help would be appreciated thanx :)
任何帮助将不胜感激thanx :)
回答by rishi
You can achieve this in following way -
您可以通过以下方式实现这一点 -
1.Create an index array that contains all the required index -
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.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.
2.对于每个索引,您需要有一个数组来显示这些部分的行。所以你可以有一个字典,其中包含一个对应于每个索引的数组。
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/
检查本教程,它将帮助您实现这一点- http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
回答by kb920
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
在合适的视图中返回单元格