IOS开发(51)之UITableView建立索引搜索

1 前言

昨天工作时候遇到TableView的建立索引问题,由于有的时候TableView之中的数据量十分之大,以至于需要在右侧建立索引来搜索,今日特意整理于下面,供大家参考,互相学习。

2 代码实例

ZYViewController.h

#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

@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];
}

运行结果:


3 结语

以上就是所有内容,希望对大家有所帮助

Demo下载地址:http://download.csdn.net/detail/u010013695/5347887

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值