前言
目前项目中提出
iOS
默认索引,如果index
个数太少,间隔太紧密。让向android
靠拢。
用法
使用的是DeanKit
效果
引入源码
源码简介
DSectionIndexView
为索引部分
添加头文件
#import "DSectionIndexItemView.h"
#import "DSectionIndexView.h"
实现协议
索引个数
- (NSInteger)numberOfItemViewForSectionIndexView:(DSectionIndexView *)sectionIndexView
{
return self.tableview.numberOfSections;
}
自定义索引ItemView
- (DSectionIndexItemView *)sectionIndexView:(DSectionIndexView *)sectionIndexView itemViewForSection:(NSInteger)section
{
DSectionIndexItemView *itemView = [[DSectionIndexItemView alloc] init];
itemView.titleLabel.text = [self.sections objectAtIndex:section];
itemView.titleLabel.font = [UIFont systemFontOfSize:12];
itemView.titleLabel.textColor = [UIColor darkGrayColor];
itemView.titleLabel.highlightedTextColor = [UIColor redColor];
itemView.titleLabel.shadowColor = [UIColor whiteColor];
itemView.titleLabel.shadowOffset = CGSizeMake(0, 1);
return itemView;
}
自定义callout
- (UIView *)sectionIndexView:(DSectionIndexView *)sectionIndexView calloutViewForSection:(NSInteger)section
{
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, 0, 80, 80);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor redColor];
label.font = [UIFont boldSystemFontOfSize:36];
label.text = [self.sections objectAtIndex:section];
label.textAlignment = UITextAlignmentCenter;
[label.layer setCornerRadius:label.frame.size.width/2];
[label.layer setBorderColor:[UIColor lightGrayColor].CGColor];
[label.layer setBorderWidth:3.0f];
[label.layer setShadowColor:[UIColor blackColor].CGColor];
[label.layer setShadowOpacity:0.8];
[label.layer setShadowRadius:5.0];
[label.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
return label;
}
index
的title
- (NSString *)sectionIndexView:(DSectionIndexView *)sectionIndexView
titleForSection:(NSInteger)section
{
return [self.sections objectAtIndex:section];
}
选中某个index
- (void)sectionIndexView:(DSectionIndexView *)sectionIndexView didSelectSection:(NSInteger)section
{
[self.tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
设置frame
#define kSectionIndexWidth 40.f
#define kSectionIndexHeight 360.f
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
_sectionIndexView.frame = CGRectMake(CGRectGetWidth(self.tableview.frame) - kSectionIndexWidth, (CGRectGetHeight(self.tableview.frame) - kSectionIndexHeight)/2, kSectionIndexWidth, kSectionIndexHeight);
[_sectionIndexView setBackgroundViewFrame];
}
reload
加载完数据,需要reloadindex
.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.sectionIndexView reloadItemViews];
}