热门搜索 :
考研考公
您的当前位置:首页正文

UITableView+Extension

来源:东饰资讯网

主要写的是UITableView的索引,我们知道UItableview自带的索引比较难看,所以找资料写了这个.. 废话不多说,直接上代码

UITableView+Extension的.h部分:

#import   <uikit/uikit.h>

@class YWIndexView;

@interface UITableView (Extension)

-(UIView *)setIndexViewWithSArr:(NSArray *)titleArr;

@end

@interface YWIndexView : UIView

@property(nonatomic,strong)NSMutableArray *titles;

@end

UITableView+Extension的.m部分:

#import "UITableView+Extension.h"

static  float number;

@implementation UITableView (Extension)

-(UIView *)setIndexViewWithSArr:(NSMutableArray *)titleArr{

float tableViewH = self.bounds.size.height;

float insetTop  = self.contentInset.top;

float insetBottom = self.contentInset.bottom;

float screenW    = [[UIScreen mainScreen] bounds].size.width;

float indexViewH = (tableViewH - insetTop - insetBottom)*0.73;

float indexViewY = (tableViewH - insetTop - insetBottom)*0.09 *0.5+self.frame.origin.y +insetTop;

YWIndexView *indexView = [[YWIndexView alloc]initWithFrame:CGRectMake(screenW - 15, indexViewY, 10, indexViewH)];

indexView.titles = titleArr;

number = titleArr.count;

for (int i = 0; i < titleArr.count; i++) {

UILabel *letterLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, indexViewH/number*i,12, indexViewH/number)];

letterLabel.font = [UIFont systemFontOfSize:12];

letterLabel.textColor = [UIColor grayColor];

letterLabel.text = titleArr[i];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(indexViewTap:)];

[indexView addGestureRecognizer:tap];

[indexView addSubview:letterLabel];

}

if (self.superview) {

[self.superview addSubview:indexView];

}

else{

return nil;

}

return indexView;

}

-(void)indexViewTap:(UITapGestureRecognizer *)tap{

// indexView

YWIndexView *label = (YWIndexView *)tap.view;

float  touchY = [tap locationInView:tap.view].y;

float index = touchY /label.bounds.size.height *(number);

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:index+2];

[self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

@end

@implementation YWIndexView

@end

他的使用方法:

  主要代码部分:

[self.view addSubview:_classifyTableVC];

UIView *subview =  [_classifyTableVC setIndexViewWithSArr:Title];

[self.view addSubview:subview];

Top