IOS uiscrollview content reuse [to realize the reuse of two attempts]


The content reuse here refers to the attempt to add to the ScrollView is the same model; for example, I need to add 100 xkviews (other packaged VC and UIView) from the ScrollView. Each time I slide the ScrollView, I only need to update the content on xkVIew; when there are more ScrollView contents, I can consider reuse.

Recently, I have done the layout of test papers. During the presentation of test papers, I packaged a VC based test model, PaperQuestionViewController (used to display the content of each test paper. index attribute should be added to the template for reuse). Because a set of test papers will have 100 + test questions. If you put 100 questions at once+ Try to add to ScrollView at the same time, so the memory is large, which is the most important reason for reuse;


Current VC.m

///All test questions array
@property (nonatomic,strong) NSArray *arrayQuestin;

@property (nonatomic,strong) UIScrollView *scrollview;

///Save visible views
@property (nonatomic, strong) NSMutableSet *visibleViewControllers;

/// Save reusable
@property (nonatomic, strong) NSMutableSet *reusedViewControllers;

Reference ScrollView agent


Implementing agent methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    ///Update template information
    [self showVc];

Additional method

///Display attempt
- (void)showVc{
    // Gets the index of the controller currently in the display range
    CGRect visibleBounds = self.scrollview.bounds;
    CGFloat minX  = CGRectGetMinX(visibleBounds);
    CGFloat maxX  = CGRectGetMaxX(visibleBounds);
    CGFloat width = CGRectGetWidth(visibleBounds);
    NSInteger firstIndex = (NSInteger)floorf(minX / width);
    NSInteger lastIndex  = (NSInteger)floorf(maxX / width);
    // Dealing with cross-border issues
    if (firstIndex < 0) {
        firstIndex = 0;
    if (lastIndex >= self.arrayQuestin.count) {
        lastIndex = (self.arrayQuestin.count - 1);
    // Recycle not shown
    NSInteger viewIndex = 0;
    for (PaperQuestionViewController * vc in self.visibleViewControllers) {
        viewIndex = vc.index;
        // Not in display range
        if ( viewIndex < firstIndex || viewIndex > lastIndex) {
            [self.reusedViewControllers addObject:vc];
            [vc removeFromParentViewController];
            [vc.view removeFromSuperview];
    [self.visibleViewControllers minusSet:self.reusedViewControllers];
    // Whether a new view needs to be displayed
    for (NSInteger index = firstIndex; index <= lastIndex; index ++) {
        BOOL isShow = NO;
        for (BookPaperQuestionViewController * childVc in self.visibleViewControllers) {
            if (childVc.index == index) {
                isShow = YES;
        if (!isShow ) {
            [self showVcWithIndex:index];

// Show a view
- (void)showVcWithIndex:(NSInteger)index{
    PaperQuestionViewController *vc = [self.reusedViewControllers anyObject];
    if (vc) {
        [self.reusedViewControllers removeObject:vc];
        PaperQuestionViewController *childVc = [[PaperQuestionViewController alloc] init];
        [self addChildViewController:childVc];
        vc = childVc;
    CGRect bounds  = self.scrollview.bounds;//654
    CGRect vcFrame = bounds;
    vcFrame.origin.x = CGRectGetWidth(bounds) * index;
    vc.rectView = vcFrame;
    vc.index = index;
    vc.view.frame = vcFrame;
    // Finally, in this place, update the template VC Information in
    ///Update information processing



Tags: iOS Attribute

Posted on Fri, 03 Apr 2020 15:42:50 -0700 by gintjack