A-Z表格索引
#import "SGViewController.h"
@interface SGViewController (){
NSMutableArray *_indexTitleArr; //索引數組
NSMutableArray *_titleArray; //表中內容
UITableView *_myTableView;
}
@end
@implementation SGViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_myTableView.sectionIndexColor = [UIColor blueColor];
_myTableView.sectionIndexTrackingBackgroundColor = [UIColor grayColor];
_myTableView.sectionIndexBackgroundColor = [UIColor clearColor];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
_titleArray = [NSMutableArray array];
_indexTitleArr = [NSMutableArray array];
for(char c = 'A'; c <= 'Z'; c++ )
{
[_indexTitleArr addObject:[NSString stringWithFormat:@"%c",c]];
[_titleArray addObject:[NSString stringWithFormat:@"%c",c]];
[_titleArray addObject:[NSString stringWithFormat:@"%c",c]];
}
//初始化數據
[self initMyTableView];
}
//初始化UITableView
- (void)initMyTableView{
_myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20) style:UITableViewStylePlain];
_myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
_myTableView.showsVerticalScrollIndicator = NO;
[_myTableView setDataSource:self];
[_myTableView setDelegate:self];
[self.view addSubview:_myTableView];
}
#pragma mark UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *Identify = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identify];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identify];
}
cell.textLabel.text = [_titleArray objectAtIndex:indexPath.section * 2 + indexPath.row];
return cell;
}
//前往section中的row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 2;
}
//前往索引數組
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return _indexTitleArr;
}
//前往每個索引的內容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [_indexTitleArr objectAtIndex:section];
}
//前往section的個數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [_indexTitleArr count];
}
//呼應點擊索引時的委托辦法
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
NSInteger count = 0;
for (NSString *character in _indexTitleArr) {
if ([[character uppercaseString] hasprefix:title]) {
return count;
}
count++;
}
return 0;
}
@end
【iOS_表格索引】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!