IOS5基礎之十二-----分組分區和索引分區

這個分區很想我們手機上通訊錄的樣式。所以說用處還是很大的哦~~~~~

最近在家待了幾天感覺好像還有點感冒了,哈哈,所以,還是要出去走走,不要當宅男哦,還是很容易生病的哦~~~~~~

其實這個還是比較簡單的,我就直接放代碼了,前面直接就是託控件,關聯一個委託和數據源。

頭文件代碼

#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只是樣式上面有區別,就是看個人如何選擇和客戶如何要求,其實自己也不是很清楚了~~~~~~


有難度的是如何將有搜索欄的並能查詢。

發佈了102 篇原創文章 · 獲贊 35 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章