自己写的一个tableView自定义索引栏

由于项目美工的强烈要求,根据她的要求写了一个自定义的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;


点m文件

- (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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值