1 前言
昨天工作時候遇到TableView的建立索引問題,由於有的時候TableView之中的數據量十分之大,以至於需要在右側建立索引來搜索,今日特意整理於下面,供大家參考,互相學習。
2 代碼實例
ZYViewController.h
[plain]
#import <UIKit/UIKit.h>
@interface ZYViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
//設置索引標題
@property(nonatomic,retain)NSMutableArray *indexArray;
//設置每個section下的cell內容
@property(nonatomic,retain)NSArray *dataArray1;
@property(nonatomic,retain)NSArray *dataArray2;
@property(nonatomic,retain)NSArray *dataArray3;
@end
#import <UIKit/UIKit.h>
@interface ZYViewController : UITableViewController
<UITableViewDataSource, UITableViewDelegate>
//設置索引標題
@property(nonatomic,retain)NSMutableArray *indexArray;
//設置每個section下的cell內容
@property(nonatomic,retain)NSArray *dataArray1;
@property(nonatomic,retain)NSArray *dataArray2;
@property(nonatomic,retain)NSArray *dataArray3;
@end
ZYViewController.m
[plain]
@synthesize dataArray1,dataArray2,dataArray3;
@synthesize indexArray;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array1=[[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E" ,nil];
NSArray *array2=[[NSArray alloc] initWithObjects:@"F",@"G",@"H",@"I",@"J", nil];
NSArray *array3=[[NSArray alloc] initWithObjects:@"K",@"L",@"M",@"N",@"O", nil];
self.dataArray1=array1;
self.dataArray2 = array2;
self.dataArray3 = array3;
//給數組賦值
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];
self.indexArray = array;
[array release];
[indexArray addObjectsFromArray:array1];
[indexArray addObjectsFromArray:array2];
[indexArray addObjectsFromArray:array3];
[array1 release];
[array2 release];
[array3 release];
}
//設置Section的Header的值
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
NSString *key = [indexArray objectAtIndex:section];
return key;
}
#pragma mark -
#pragma mark Table View Data Source Methods
//設置表格的索引數組
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexArray;
}
////允許數據源告知必須加載到Table View中的表的Section數。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//設置表格的行數為數組的元素個數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return [self.dataArray1 count];
}else if(section==1)
return dataArray2.count;
else
return dataArray3.count;
}
//每一行的內容為數組相應索引的值
// Customize the appearance of table view cells.
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.section==0)
//設置單元格的字符串內容
cell.textLabel.text=[self->dataArray1 objectAtIndex:indexPath.row];
else if(indexPath.section==1)
//設置單元格的字符串內容
cell.textLabel.text=[self->dataArray2 objectAtIndex:indexPath.row];
else
//設置單元格的字符串內容
cell.textLabel.text=[self->dataArray3 objectAtIndex:indexPath.row];
return cell;
}
-(void)dealloc{
[indexArray release];
[dataArray1 release];
[dataArray2 release];
[dataArray3 release];
[super dealloc];
}
@synthesize dataArray1,dataArray2,dataArray3;
@synthesize indexArray;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array1=[[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E" ,nil];
NSArray *array2=[[NSArray alloc] initWithObjects:@"F",@"G",@"H",@"I",@"J", nil];
NSArray *array3=[[NSArray alloc] initWithObjects:@"K",@"L",@"M",@"N",@"O", nil];
self.dataArray1=array1;
self.dataArray2 = array2;
self.dataArray3 = array3;
//給數組賦值
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];
self.indexArray = array;
[array release];
[indexArray addObjectsFromArray:array1];
[indexArray addObjectsFromArray:array2];
[indexArray addObjectsFromArray:array3];
[array1 release];
[array2 release];
[array3 release];
}
//設置Section的Header的值
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
NSString *key = [indexArray objectAtIndex:section];
return key;
}
#pragma mark -
#pragma mark Table View Data Source Methods
//設置表格的索引數組
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexArray;
}
////允許數據源告知必須加載到Table View中的表的Section數。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//設置表格的行數為數組的元素個數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return [self.dataArray1 count];
}else if(section==1)
return dataArray2.count;
else
return dataArray3.count;
}
//每一行的內容為數組相應索引的值
// Customize the appearance of table view cells.
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.section==0)
//設置單元格的字符串內容
cell.textLabel.text=[self->dataArray1 objectAtIndex:indexPath.row];
else if(indexPath.section==1)
//設置單元格的字符串內容
cell.textLabel.text=[self->dataArray2 objectAtIndex:indexPath.row];
else
//設置單元格的字符串內容
cell.textLabel.text=[self->dataArray3 objectAtIndex:indexPath.row];
return cell;
}
-(void)dealloc{
[indexArray release];
[dataArray1 release];
[dataArray2 release];
[dataArray3 release];
[super dealloc];
}
運行結果: