美文网首页
实现一个简单的重用池

实现一个简单的重用池

作者: 动感超人丶 | 来源:发表于2019-03-29 10:18 被阅读0次

.h文件

//
//  THReusePool.h
//  into
//
//  Created by huant on 2019/3/29.
//  Copyright © 2019 huant. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface THReusePool : NSObject

- (UIView *)dequeReuseView;


- (void)addViewToolPool:(UIView *)view;

- (void)resetPool;
@end

NS_ASSUME_NONNULL_END

.m文件

//
//  THReusePool.m
//  into
//
//  Created by huant on 2019/3/29.
//  Copyright © 2019 huant. All rights reserved.
//

#import "THReusePool.h"

@interface THReusePool ()

@property (nonatomic, strong) NSMutableSet * waitingPool;

@property (nonatomic, strong) NSMutableSet * usingPool;

@end
@implementation THReusePool


- (instancetype)init{
    if (self = [super init]) {
        
        self.waitingPool = [NSMutableSet set];
        self.usingPool = [NSMutableSet set];
    }
    return self;
}

- (UIView *)dequeReuseView{
    
    UIView * viewPool = [self.waitingPool anyObject];
    
    if (!viewPool) {
        return nil;
    }else{
        NSLog(@"从pool中取出视图");
        [self.usingPool addObject:viewPool];
        [self.waitingPool removeObject:viewPool];
        return viewPool;
    }
}


- (void)addViewToolPool:(UIView *)view{
    NSLog(@"添加视图到pool中");

    [self.usingPool addObject:view];
}

- (void)resetPool{
    
    UIView * view = nil;
    while ([self.usingPool count]) {
        
        view = [self.usingPool anyObject];
        [self.waitingPool addObject:view];
        [self.usingPool removeObject:view];
    }
    NSLog(@"pool中可重用%lu个视图", (unsigned long)self.waitingPool.count);

}



@end

外面调用测试

   if (flag == 0) {
        flag = 1;
        for (int i = 0; i<5; i++) {
            
            UILabel * view = (UILabel *)[self.pool dequeReuseView];
            if (!view) {
                view = [UILabel new];
                [self.pool addViewToolPool:view];
            }
            view.frame = CGRectMake(0, i*20, 100, 20);
            view.text = [NSString stringWithFormat:@"%d", i];
            [self.view addSubview:view];
        }
        
    }else{
        flag = 0;
        
        for (int i = 0; i<10; i++) {
            
            UILabel * view = (UILabel *)[self.pool dequeReuseView];
            if (!view) {
                view = [UILabel new];
                [self.pool addViewToolPool:view];
            }
            view.frame = CGRectMake(0, i*20, 100, 20);
            view.text = [NSString stringWithFormat:@"%d", i];
            [self.view addSubview:view];
        }
    }
    

相关文章

  • 实现一个简单的重用池

    .h文件 .m文件 外面调用测试

  • UI篇

    1、什么是重用机制?通过identifer作为标识来创建不同的cell,依托重用池来实现cell的重用 2、UI数...

  • java Executor

    一、什么是Executor框架? 我们知道线程池就是线程的集合,线程池集中管理线程,以实现线程的重用,降低资源消耗...

  • 重用池

    网站: http://div.gxzj.com.cn/News.aspx?id=225626 下面是转载: 常规配...

  • 重用机制原理

    重用池:2个NSMutableSet的队列,一个等待使用队列,一个使用中的队列。 自定义的重用池使用方法: 自定义...

  • 模块

    重用一个代码块是通过定义函数来实现,要重用一系列代码块,则通过定义模块来实现。 编写模块有很多种方法,其中最简单的...

  • 第一篇:Objective-C 知识回顾的UI视图部分之一

    1.1.UITableView 重用机制 核心知识点 1 -- 重用池 模拟 UITableView 的重用机制,...

  • TableView重用原理及一些注意点

    什么是cell的重用?# 重用机制,简单的说意思是一行一行cell的复用 为什么要实现重用?# 为了做到显示和数据...

  • 线程池的基本使用

    说起线程池,我们先来简单说一下线程池的好处:a. 重用存在的线程,减少对象创建、消亡的开销,性能佳。b. 可有效控...

  • 线程池

    线程池 项目文件:HelloJava-ThreadPoolExecutorDemo 线程池优点:重用线程,避免创建...

网友评论

      本文标题:实现一个简单的重用池

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