#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