前言:最近接了一個新項目、項目中有一個需求就是一個UITableView右側(cè)滑動索引定位TableView分區(qū)內(nèi)容、好了、相信大家對這個功能點都有所了解以及見過、那么做成微信的形式咱們是不是得自定義呀、好了、上代碼。
#pragma mark - UITableView Delegate
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
static NSString *identfier = @"XCLeagueFilterCell";
XCLeagueFilterCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
if (cell == nil) {
cell = [[XCLeagueFilterCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identfier];
}
NSString *key = self.groupNameArr[indexPath.section];
NSArray *valueArr = [self.dataDic objectForKey:key];
cell.titlelabel.text = [NSString stringWithFormat:@"%@", valueArr[indexPath.row]];
cell.deatilLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row];
// 第一行顯示
if (indexPath.row == 0) {
cell.topLineLabel.hidden = NO;
} else {
cell.topLineLabel.hidden = YES;
}
[cell.logonIv setImage:XCImage(@"score_icon_liansai")];
return cell;
}
#pragma mark 索引列點擊事件
// 手指滑動及點擊字母相應的代理
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
//點擊索引,列表跳轉(zhuǎn)到對應索引的行
if ([title isEqualToString:@"熱門"]) {
return self.groupNameArr.count;
}
[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
//[XCProgressHUD showSuccess:title];
return index;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.groupNameArr objectAtIndex:section];
}
// 索引標題就是右側(cè)的A-Z 字母
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
NSMutableArray *listTitlesArray = [[NSMutableArray alloc] initWithCapacity:[self.groupNameArr count]];
for (NSString *item in self.groupNameArr) {
[listTitlesArray addObject:item];
}
return listTitlesArray;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *key = self.groupNameArr[section];
NSArray *valueArr = [self.dataDic objectForKey:key];
return valueArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.groupNameArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
圖片中已經(jīng)注釋了幾個重點的代理方法、如需要其他功能定制則實現(xiàn)系統(tǒng)其他代理方法。下載地址:https://github.com/chenxuhunoc/WeChatDragIndex