由于项目美工的强烈要求,根据她的要求写了一个自定义的tableview索引栏,下面是我的代码
/**
* 数据数组:manager.listArray
* 索引名称数组:sectionArr
* tableView行高:RowHeight
* tableView的sectionView高:SectionHeight
*/
if (iPhone5) {
indexView = [[IndexView alloc]initWithFrame:CGRectMake(287, 5, 30, 440) WithDataArray:manager.listArray WithNameArray:sectionArr WithRowHeight:50.0 WithSectionHeight:20.0];
}else{
indexView = [[IndexView alloc]initWithFrame:CGRectMake(287, 5, 30, 360) WithDataArray:manager.listArray WithNameArray:sectionArr WithRowHeight:50.0 WithSectionHeight:20.0];
}
indexView.delegate = self;
[self.view addSubview:indexView];
点h文件
@protocol IndexViewDelegate <NSObject>
@optional
//代理方法
- (void)indexViewChangeToRect:(CGPoint)point;
@end
@interface IndexView : UIImageView
{
NSMutableArray *indexArr;
float indexRowHeight;
float indexSectionHeight;
}
@property(nonatomic,strong)id<IndexViewDelegate>delegate;
- (id)initWithFrame:(CGRect)frame WithDataArray:(NSMutableArray *)arr WithNameArray:(NSMutableArray *)nameArr WithRowHeight:(float)rowHeight WithSectionHeight:(float)sectionHeight;
- (id)initWithFrame:(CGRect)frame WithDataArray:(NSMutableArray *)arr WithNameArray:(NSMutableArray *)nameArr WithRowHeight:(float)rowHeight WithSectionHeight:(float)sectionHeight;
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = YES;//开启交互
indexArr = arr;
indexRowHeight = rowHeight;
indexSectionHeight = sectionHeight;
//由于需要定义文字大小,所以加入了label
for (int i = 0 ; i<arr.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = i+2000;
btn.frame = CGRectMake(0, 0+i*(self.frame.size.height/arr.count), self.frame.size.width, self.frame.size.height/arr.count);
[btn addTarget:self action:@selector(changeToRect:) forControlEvents:UIControlEventAllTouchEvents];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, btn.frame.size.width, btn.frame.size.height)];
label.text = [nameArr objectAtIndex:i];
label.tag = btn.tag + 1;
label.font = [UIFont systemFontOfSize:12];
[label.layer setMasksToBounds:YES];
label.layer.cornerRadius = 5;
label.textAlignment = NSTextAlignmentCenter;
label.textColor = RGBCOLOR(80, 80, 80);
label.backgroundColor = [UIColor clearColor];
[btn addSubview:label];
[self addSubview:btn];
}
}
return self;
}
static int tagL;
- (void)changeToRect:(UIButton *)btn{
//记录上次的按钮,然后取消背景颜色
if (tagL!=0) {
UILabel *label = (UILabel *)[self viewWithTag:tagL];
label.backgroundColor = [UIColor clearColor];
}
UILabel *label = (UILabel *)[btn viewWithTag:btn.tag+1];
tagL = btn.tag+1;
label.backgroundColor = RGBCOLOR(255, 212, 150);
float num = 0.0;
//计算需要跳转到的位置
for (int i = 0; i < btn.tag - 2000; i++) {
num+= ([[indexArr objectAtIndex:i] count]*indexRowHeight) + indexSectionHeight+2;
}//+2是因为分割线
CGPoint point = CGPointMake(0, num);
if ([self.delegate respondsToSelector:@selector(indexViewChangeToRect:)]) {
[self.delegate indexViewChangeToRect:point];
}
}
#pragma mark - IndexView的代理方法
//在代理里跳转位置
- (void)indexViewChangeToRect:(CGPoint)point{
[fontListTableView setContentOffset:point animated:YES];
}
这样就ok啦,效果如下:
由于项目需求有限,我这个索引比较简单,之后找到了一个更好的,地址如下:
http://code4app.com/ios/MJNIndexView/522008c56803fafe03000002