/********************************* 点击右侧索引,居中显示相应索引 ********************************/
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) UIView *tipsView;
@property (nonatomic, strong) UILabel *tipsLab;
@property (nonatomic, strong) NSTimer *timer;
/********************************* 点击右侧索引,居中显示相应索引 ********************************/
//设置右侧索引颜色
self.tableView.sectionIndexColor = [UIColor grayColor];
//设置选中时,索引背景颜色
//self.tableView.sectionIndexTrackingBackgroundColor = [UIColor grayColor];
//设置默认时,索引的背景颜色
//self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
//#pragma mark - 自定义sectionView
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
//{
// NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
//
// //自定义Section字体大小
// UILabel *label = [[UILabel alloc]init];
// label.frame = CGRectMake(10, 2.5, 300, 10);
// label.backgroundColor = [UIColor clearColor];
// label.textColor = [UIColor grayColor];
// label.font = [UIFont systemFontOfSize:10];
// //可以设置字体的方法
// // label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
// label.text = sectionTitle;
// //创建headerView并且把label添加进去
// UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 15)];
// [sectionView setBackgroundColor:[UIColor lightTextColor]];
// [sectionView addSubview:label];
// return sectionView;
//
//}
#pragma mark - 右侧索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//用索引时候要写上UITableViewDelegate,UITableViewDataSource
NSMutableArray *tempArr = [NSMutableArray array];
[tempArr addObject:[NSString stringWithFormat:@"↑"]];
[tempArr addObject:[NSString stringWithFormat:@"☆"]];
for (char c = 'A'; c <= 'Z'; c++) {
NSString *currentLetter = [NSString stringWithFormat:@"%c",c];
[tempArr addObject:currentLetter];
}
[tempArr addObject:@"#"];
NSLog(@"%lu",(unsigned long)tempArr.count);
return tempArr;
// return [self.cityGroups valueForKeyPath:@"label"];
}
#pragma mark - 索引列点击事件
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
// NSLog(@"===%@ ===%ld",title,(long)index);
// //点击索引,列表跳转到对应索引的行
// [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
// return index;
[self showTipsWithTitle:title];
return index;
}
#pragma mark - 点击右侧索引时,居中显示相应索引字母
- (void)showTipsWithTitle:(NSString*)title
{
//获取当前屏幕window
UIWindow *window = [UIApplication sharedApplication].keyWindow;
//添加黑色透明背景
// if (!_bgImageView) {
// _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width, window.frame.size.height)];
// _bgImageView.backgroundColor = [UIColor blackColor];
// _bgImageView.alpha = 0.1;
// [window addSubview:_bgImageView];
// }
if (!_tipsView) {
//添加字母提示框
_tipsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
_tipsView.center = window.center;
_tipsView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:0.8];
//_tipsView.backgroundColor = Color(241, 241, 241);
//设置提示框圆角
_tipsView.layer.masksToBounds = YES;
_tipsView.layer.cornerRadius = _tipsView.frame.size.width/20;
_tipsView.layer.borderColor = [UIColor whiteColor].CGColor;
_tipsView.layer.borderWidth = 2;
[window addSubview:_tipsView];
}
if (!_tipsLab) {
//添加提示字母lable
_tipsLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _tipsView.frame.size.width, _tipsView.frame.size.height)];
//设置背景为透明
_tipsLab.backgroundColor = [UIColor clearColor];
_tipsLab.font = [UIFont boldSystemFontOfSize:50];
_tipsLab.textAlignment = NSTextAlignmentCenter;
[_tipsView addSubview:_tipsLab];
}
_tipsLab.text = title;//设置当前显示字母
// [self performSelector:@selector(hiddenTipsView:) withObject:nil afterDelay:0.3];
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self hiddenTipsView];
// });
_timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(hiddenTipsView) userInfo:nil repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
#pragma mark - 隐藏 居中显示的索引
- (void)hiddenTipsView
{
[UIView animateWithDuration:0.2 animations:^{
_bgImageView.alpha = 0;
_tipsView.alpha = 0;
} completion:^(BOOL finished) {
[_bgImageView removeFromSuperview];
[_tipsView removeFromSuperview];
_bgImageView = nil;
_tipsLab = nil;
_tipsView = nil;
}];
}