#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
// 實(shí)現(xiàn)重用機(jī)制的類
@interface ViewReusePool : NSObject
// 從重用池當(dāng)中取出一個(gè)可重用的view
- (UIView *)dequeueReusableView;
// 向重用池當(dāng)中添加一個(gè)視圖
- (void)addUsingView:(UIView *)view;
// 重置方法疏虫,將當(dāng)前使用中的視圖移動(dòng)到可重用隊(duì)列當(dāng)中
- (void)reset;
@end
#import "ViewReusePool.h"
@interface ViewReusePool ()
// 等待使用的隊(duì)列
@property (nonatomic, strong) NSMutableSet *waitUsedQueue;
// 使用中的隊(duì)列
@property (nonatomic, strong) NSMutableSet *usingQueue;
@end
@implementation ViewReusePool
- (id)init{
self = [super init];
if (self) {
_waitUsedQueue = [NSMutableSet set];
_usingQueue = [NSMutableSet set];
}
return self;
}
- (UIView *)dequeueReusableView{
UIView *view = [_waitUsedQueue anyObject];
if (view == nil) {
return nil;
}
else{
// 進(jìn)行隊(duì)列移動(dòng)
[_waitUsedQueue removeObject:view];
[_usingQueue addObject:view];
return view;
}
}
- (void)addUsingView:(UIView *)view
{
if (view == nil) {
return;
}
// 添加視圖到使用中的隊(duì)列
[_usingQueue addObject:view];
}
- (void)reset{
UIView *view = nil;
while ((view = [_usingQueue anyObject])) {
// 從使用中隊(duì)列移除
[_usingQueue removeObject:view];
// 加入等待使用的隊(duì)列
[_waitUsedQueue addObject:view];
}
}
@end
#import <UIKit/UIKit.h>
@protocol IndexedTableViewDataSource <NSObject>
// 獲取一個(gè)tableview的字母索引條數(shù)據(jù)的方法
- (NSArray <NSString *> *)indexTitlesForIndexTableView:(UITableView *)tableView;
@end
@interface IndexedTableView : UITableView
@property (nonatomic, weak) id <IndexedTableViewDataSource> indexedDataSource;
@end
#import "IndexedTableView.h"
#import "ViewReusePool.h"
@interface IndexedTableView ()
{
UIView *containerView;
ViewReusePool *reusePool;
}
@end
@implementation IndexedTableView
- (void)reloadData{
[super reloadData];
// 懶加載
if (containerView == nil) {
containerView = [[UIView alloc] initWithFrame:CGRectZero];
containerView.backgroundColor = [UIColor whiteColor];
//避免索引條隨著table滾動(dòng)
[self.superview insertSubview:containerView aboveSubview:self];
}
if (reusePool == nil) {
reusePool = [[ViewReusePool alloc] init];
}
// 標(biāo)記所有視圖為可重用狀態(tài)
[reusePool reset];
// reload字母索引條
[self reloadIndexedBar];
}
- (void)reloadIndexedBar
{
// 獲取字母索引條的顯示內(nèi)容
NSArray <NSString *> *arrayTitles = nil;
if ([self.indexedDataSource respondsToSelector:@selector(indexTitlesForIndexTableView:)]) {
arrayTitles = [self.indexedDataSource indexTitlesForIndexTableView:self];
}
// 判斷字母索引條是否為空
if (!arrayTitles || arrayTitles.count <= 0) {
[containerView setHidden:YES];
return;
}
NSUInteger count = arrayTitles.count;
CGFloat buttonWidth = 60;
CGFloat buttonHeight = self.frame.size.height / count;
for (int i = 0; i < [arrayTitles count]; i++) {
NSString *title = [arrayTitles objectAtIndex:i];
// 從重用池當(dāng)中取一個(gè)Button出來
UIButton *button = (UIButton *)[reusePool dequeueReusableView];
// 如果沒有可重用的Button重新創(chuàng)建一個(gè)
if (button == nil) {
button = [[UIButton alloc] initWithFrame:CGRectZero];
button.backgroundColor = [UIColor whiteColor];
// 注冊button到重用池當(dāng)中
[reusePool addUsingView:button];
NSLog(@"新創(chuàng)建一個(gè)Button");
}
else{
NSLog(@"Button 重用了");
}
// 添加button到父視圖控件
[containerView addSubview:button];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 設(shè)置button的坐標(biāo)
[button setFrame:CGRectMake(0, i * buttonHeight, buttonWidth, buttonHeight)];
}
[containerView setHidden:NO];
containerView.frame = CGRectMake(self.frame.origin.x + self.frame.size.width - buttonWidth, self.frame.origin.y, buttonWidth, self.frame.size.height);
}
@end
#import "ViewController.h"
#import "IndexedTableView.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,IndexedTableViewDataSource>
{
IndexedTableView *tableView;//帶有索引條的tableview
UIButton *button;
NSMutableArray *dataSource;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建一個(gè)Tableview
tableView = [[IndexedTableView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
// 設(shè)置table的索引數(shù)據(jù)源
tableView.indexedDataSource = self;
[self.view addSubview:tableView];
//創(chuàng)建一個(gè)按鈕
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
button.backgroundColor = [UIColor redColor];
[button setTitle:@"reloadTable" forState:UIControlStateNormal];
[button addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// 數(shù)據(jù)源
dataSource = [NSMutableArray array];
for (int i = 0; i < 100; i++) {
[dataSource addObject:@(i+1)];
}
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark IndexedTableViewDataSource
- (NSArray <NSString *> *)indexTitlesForIndexTableView:(UITableView *)tableView{
//奇數(shù)次調(diào)用返回6個(gè)字母硫兰,偶數(shù)次調(diào)用返回11個(gè)
static BOOL change = NO;
if (change) {
change = NO;
return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K"];
}
else{
change = YES;
return @[@"A",@"B",@"C",@"D",@"E",@"F"];
}
}
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dataSource count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"reuseId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//如果重用池當(dāng)中沒有可重用的cell懒叛,那么創(chuàng)建一個(gè)cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// 文案設(shè)置
cell.textLabel.text = [[dataSource objectAtIndex:indexPath.row] stringValue];
//返回一個(gè)cell
return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}
- (void)doAction:(id)sender{
NSLog(@"reloadData");
[tableView reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end