iOS中UITableView的索引設定
看到很多app的關於UITableView的頁面在右手邊,都有一系列的索引設定;簡單的學習了下,其實主要是呼叫了UITableView的相關代理方法來實現的:
主要是實現下面四個方法:
//返回section中的row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//返回每個索引的內容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//返回索引陣列
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
//響應點選索引時的委託方法
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;
當然在UITableView中還有關於索引的相關屬性設定如下:
_myTableView.sectionIndexColor = [UIColorblueColor
_myTableView.sectionIndexTrackingBackgroundColor = [UIColorgrayColor];//設定選中時,索引背景顏色
_myTableView.sectionIndexBackgroundColor = [UIColorclearColor];// 設定預設時,索引的背景顏色
相關程式碼如下所示:
@interface ViewController (){ NSMutableArray *indexs; //索引陣列 NSMutableArray *_titleArray; //表中內容 } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; _titleArray = [NSMutableArray array]; indexs = [NSMutableArray array]; for(char c = 'A'; c <= 'Z'; c++ ) { [indexs 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.sectionIndexColor = [UIColor blueColor]; _myTableView.sectionIndexTrackingBackgroundColor = [UIColor grayColor]; _myTableView.sectionIndexBackgroundColor = [UIColor clearColor]; [_myTableView setDataSource:self]; [_myTableView setDelegate:self]; [self.view addSubview:_myTableView]; } #pragma mark UITableViewDataSource //返回section中的row -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 2; } -(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; } //返回索引陣列 -(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return indexs; } //響應點選索引時的委託方法 -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ NSInteger count = 0; for (NSString *character in indexs) { if ([[character uppercaseString] hasPrefix:title]) { return count; } count++; } return 0; } //返回每個索引的內容 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [indexs objectAtIndex:section]; } //返回section的個數 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return [indexs count]; }
效果圖如下: