美文网首页2019最新iOS面试(OC+swift)
iOS 根据重用机制简单实现tableview索引

iOS 根据重用机制简单实现tableview索引

作者: CaptainRoy | 来源:发表于2019-03-18 18:45 被阅读28次
    • ViewReusePool 类 (通过队列实现重用机制)
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    /*
     实现重用机制的类
    */
    @interface ViewReusePool : NSObject
    
    // 从重用池中取出一个可重用的view
    -(UIView *)dequeueReusableView;
    
    // 向重用池当中添加一个视图
    -(void)addUsingView:(UIView *)view;
    
    // 重置 将当前使用的视图放到可重用队列中
    -(void)reset;
    
    @end
    
    #import "ViewReusePool.h"
    
    @interface ViewReusePool ()
    
    @property(nonatomic,strong)NSMutableSet *waitUsedQueue;// 等待使用的队列
    @property(nonatomic,strong)NSMutableSet *usingQueue; // 使用中的队列
    
    @end
    
    @implementation ViewReusePool
    
    -(instancetype)init
    {
        self = [super init];
        if (self) {
            _waitUsedQueue = [[NSMutableSet alloc] init];
            _usingQueue = [[NSMutableSet alloc] init];
        }
        return self;
    }
    
    -(UIView *)dequeueReusableView
    {
        UIView *view = [_waitUsedQueue anyObject]; // 等待队列中返回任意一个view
        if (view == nil) {
            return nil;
        }
        // 队列移动
        [_waitUsedQueue removeObject:view];
        [_usingQueue addObject:view];
        return view;
    }
    
    - (void)addUsingView:(UIView *)view
    {
        if (view == nil) {
            return;
        }
        // 将视图添加到使用的队列中
        [_usingQueue addObject:view];
    }
    
    -(void)reset
    {
        UIView *view = nil;
        while ((view = [_usingQueue anyObject])) {
            
            NSLog(@"view - %@",view);
            
            // 从使用队列中移除
            [_usingQueue removeObject:view];
            // 加入等待队列中
            [_waitUsedQueue addObject:view];
        }
    }
    
    @end
    
    • IndexTableView 类(继承于uitableview,实现索引按钮的重用功能)
    #import <UIKit/UIKit.h>
    
    @protocol IndexTableViewDataSource <NSObject>
    
    // 获取tableview的字母索引数据的方法
    -(NSArray *)indexTitlesForIndexTableView:(UITableView *)tableView;
    
    @end
    
    @interface IndexTableView : UITableView
    
    @property(nonatomic,weak)id<IndexTableViewDataSource> indexDataSource;
    
    @end
    
    #import "IndexTableView.h"
    #import "ViewReusePool.h"
    
    @interface IndexTableView ()
    
    @property(nonatomic,strong)UIView *containerView;
    @property(nonatomic,strong)ViewReusePool *reusePool;
    
    @end
    
    @implementation IndexTableView
    
    -(void)reloadData
    {
        [super reloadData];
        
        if (_containerView == nil) {
            _containerView = [[UIView alloc] initWithFrame:CGRectZero];
            _containerView.backgroundColor = [UIColor whiteColor];
            // 避免索引条随着table滚动
            [self.superview insertSubview:_containerView aboveSubview:self];
        }
        
        if (_reusePool == nil) {
            _reusePool = [[ViewReusePool alloc] init];
        }
        // 标记所有视图为可重用状态
        [_reusePool reset];
        
        [self reloadIndexedBar];
    }
    
    -(void)reloadIndexedBar
    {
        NSArray *arrayTitles = nil;
        if ([self.indexDataSource respondsToSelector:@selector(indexTitlesForIndexTableView:)]) {
            arrayTitles = [self.indexDataSource indexTitlesForIndexTableView:self];
        }
        
        // 判断字母索引是否为空
        if (!arrayTitles || arrayTitles.count <= 0) {
            [_containerView setHidden:YES];
            return;
        }
        
        NSUInteger count = arrayTitles.count;
        CGFloat buttonWidth = 60.0f;
        CGFloat buttonHeight = self.frame.size.height / count;
        for (NSInteger i = 0; i < arrayTitles.count; i++) {
            NSString *title = [arrayTitles objectAtIndex:i];
            // 从重用池取出一个button来
            UIButton *button = (UIButton *)[_reusePool dequeueReusableView];
            if (button == nil) {
                button = [[UIButton alloc] initWithFrame:CGRectZero];
                button.backgroundColor = [UIColor whiteColor];
                [_reusePool addUsingView:button];
                NSLog(@"创建一个button");
            } else {
                NSLog(@"button重用了");
            }
            [_containerView addSubview:button];
            [button setTitle:title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            button.frame = 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);
    }
    
    • ViewController 实现
    @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,IndexTableViewDataSource>
    
    @property(nonatomic,strong)IndexTableView *tableView;
    @property(nonatomic,strong)UIButton *button;
    @property(nonatomic,copy)NSMutableArray *dataSource;
    
    @end
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        // 创建一个tableview
        _tableView = [[IndexTableView alloc] initWithFrame:CGRectMake(0.0f, 60.0f, self.view.frame.size.width, self.view.frame.size.height - 60.0f) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.indexDataSource = self;
        [self.view addSubview:_tableView];
        
        // 创建一个按钮
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(0.0f, 20.0f, self.view.frame.size.width, 40.0f);
        _button.backgroundColor = [UIColor redColor];
        [_button setTitle:@"reload" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(reloadAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_button];
        
        // 数据
        _dataSource = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < 100; i++) {
            [_dataSource addObject:@(i+1)];
        }
        
    }
    
    #pragma mark - UITableViewDataSource
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _dataSource.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *identifier = @"reused";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = [[_dataSource objectAtIndex:indexPath.row] stringValue];
        return cell;
    }
    
    #pragma mark - UITableViewDelegate
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 40.0f;
    }
    
    #pragma mark - IndexTableViewDataSource
    -(NSArray *)indexTitlesForIndexTableView:(UITableView *)tableView
    {
        // 奇数次调用返回6个字母 偶数次返回11个
        static BOOL change = NO;
        if (change) {
            change = NO;
            return @[@"A",@"B",@"C",@"E",@"F",@"G",@"H",@"I",@"J",@"K"];
        } else {
            change = YES;
            return @[@"A",@"B",@"C",@"D",@"E",@"F"];
        }
    }
    
    #pragma mark - action method
    -(void)reloadAction
    {
        NSLog(@"reloadAction");
        [_tableView reloadData];
    }
    

    相关文章

      网友评论

        本文标题:iOS 根据重用机制简单实现tableview索引

        本文链接:https://www.haomeiwen.com/subject/juibmqtx.html