UI之区头表头区尾表尾快速定位A—Z


#import"ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView * tableV;

@property(nonatomic,strong)NSMutableArray * dataArr;

@end

@implementationViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    [self createTable];

    [self createDateSource];

    [self createHeaderView];

    [self createFootView];

}

-(void)createTable

{

    self.tableV=[[UITableViewalloc]initWithFrame:self.view.bounds style:0];

    self.tableV.delegate=self;

    self.tableV.dataSource=self;

    [self.view addSubview:self.tableV];

}

-(void)createDateSource

{

    self.dataArr=[NSMutableArrayarray];

    for (int i=0; i<26; i++) {

        NSMutableArray* array=[NSMutableArray new];

        for(int j=0;j<10; j++) {

            NSString* str=[NSString stringWithFormat:@"%c%d",'A'+i,j];

            [array addObject:str];

        }

        [self.dataArr addObject:array];

    }

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.dataArr.count;//一共有26个区

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section

{

    return[[self.dataArrobjectAtIndex:section]count];//每个区有多少行

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString * str=@"cell";

    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:str];

    if(cell==nil) {

        cell=[[UITableViewCellalloc]initWithStyle:0 reuseIdentifier:str];

    }

    cell.textLabel.text=[[self.dataArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];

    returncell;

}

 

-(NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section

{

    return [NSString stringWithFormat:@"区头%c",'A'+section];

}

-(NSString *)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section

{

    return [NSString stringWithFormat:@"区尾%c",'A'+section];

}

-(void)createHeaderView

{

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];//表头高度

    view.backgroundColor=[UIColor redColor];

    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(20, 40, 200, 20)];

    label.text=@"这个表头,不是区头";

    [view addSubview:label];

    self.tableV.tableHeaderView=view;//表视图的头视图

}

-(void)createFootView

{

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];

    view.backgroundColor=[UIColor redColor];

    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(20, 40, 200, 20)];

    label.text=@"这个表尾,不是区尾";

    [view addSubview:label];

    self.tableV.tableFooterView=view;//表视图的尾视图

}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    NSMutableArray* arr=[NSMutableArray new];

    for (int i=0; i<26; i++) {

        NSString* str=[NSString stringWithFormat:@"%c",'A'+i];

        [arr addObject:str];//显示区头A---Z在右侧

    }

    returnarr;

}

-(NSInteger)tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index

{

    returnindex;//点击右侧的A-Z则跳到相应的行

}

//-(BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    return NO;

//}

//-(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

//{

//    return NO;

//}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值