这个分区很想我们手机上通讯录的样式。所以说用处还是很大的哦~~~~~
最近在家待了几天感觉好像还有点感冒了,哈哈,所以,还是要出去走走,不要当宅男哦,还是很容易生病的哦~~~~~~
其实这个还是比较简单的,我就直接放代码了,前面直接就是托控件,关联一个委托和数据源。
头文件代码
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) NSDictionary *names;
@property (strong,nonatomic) NSArray *keys;
@end
m文件代码
#import "BIDViewController.h"
@implementation BIDViewController
@synthesize names;
@synthesize keys;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
//初始化数据
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *path= [[NSBundle mainBundle] pathForResource:@"sortednames" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
names=dict;
NSArray *array= [[names allKeys] sortedArrayUsingSelector:@selector(compare:)];
self.keys=array;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.names=nil;
self.keys=nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark
#pragma mark Table View Data Source Methods
//指定分区的数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [keys count];
}
//用于计算特定分区中的行数,检索与讨论中的分区对应的数组。并从该数组中返回行的数量。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *key =[keys objectAtIndex:section];
NSArray *nameSection=[names objectForKey:key];
return [nameSection count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section=[indexPath section];
NSUInteger row=[indexPath row];
NSString *key =[keys objectAtIndex:section];
NSArray *nameSection=[names objectForKey:key];
static NSString *SectionsTableIdentifier=@"SectionsTableIdentifiler";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier];
}
cell.textLabel.text=[nameSection objectAtIndex:row];
return cell;
}
//为每个分区指定一个可选的标题值,然后只返回这一组的字母就可以了
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *key=[keys objectAtIndex:section];
return key;
}
//添加索引的方法
-(NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
return keys;
}
@end
这里选成Grouped只是样式上面有区别,就是看个人如何选择和客户如何要求,其实自己也不是很清楚了~~~~~~
有难度的是如何将有搜索栏的并能查询。