高パフォーマンス異機種スクロールビュー

3445 ワード

参考ドキュメントのリンク記事は、天猫がオープンソースの異機種スクロールビューであり、天猫のリンゴ端で実現され、効率的な多重化に関連し、学習後に簡単にコードします.
異機種スクロールビューは、Tableビューの同構造スクロールビューに対して使用されます.通常のScrollViewビューが少ない場合は多重化の問題を考慮する必要はありませんが、数が増え続けると多重化が必要になります.
STEP1.
    TMMuiLazyScrollView *scrollview = [[TMMuiLazyScrollView alloc]init];
    scrollview.frame = self.view.bounds;
    //  contentSize
    scrollview.contentSize = CGSizeMake(CGRectGetWidth(self.view.bounds), 1230);
    scrollview.dataSource = self;
    [self.view addSubview:scrollview];
    
    
    //Here is frame array for test.
    //LazyScrollView must know every rect before rending.
    //       Item            
    rectArray  = [[NSMutableArray alloc] init];
    
    //  Item
    //Create a single column layout with 5 elements;
    for (int i = 0; i < 5 ; i++) {
        [rectArray addObject:[NSValue valueWithCGRect:CGRectMake(10, i *80 + 2 , self.view.bounds.size.width-20, 80-2)]];
    }
    //  item
    //Create a double column layout with 10 elements;
    for (int i = 0; i < 10 ; i++) {
        [rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%2)*self.view.bounds.size.width/2 + 3, 410 + i/2 *80 + 2 , self.view.bounds.size.width/2 -3, 80 - 2)]];
    }
    //  item
    //Create a trible column layout with 15 elements;
    for (int i = 0; i < 15 ; i++) {
        [rectArray addObject:[NSValue valueWithCGRect:CGRectMake((i%3)*self.view.bounds.size.width/3 + 1, 820 + i/3 *80 + 2 , self.view.bounds.size.width/3 -3, 80 - 2)]];
    }
    
    //STEP 3 reload LazyScrollView
    //           
    [scrollview reloadData];


STEP2.
//          
// implement datasource delegate.
- (NSUInteger)numberOfItemInScrollView:(TMMuiLazyScrollView *)scrollView
{
    return rectArray.count;
}
//  index  rectModel
- (TMMuiRectModel *)scrollView:(TMMuiLazyScrollView *)scrollView rectModelAtIndex:(NSUInteger)index
{
    CGRect rect = [(NSValue *)[rectArray objectAtIndex:index]CGRectValue];
    TMMuiRectModel *rectModel = [[TMMuiRectModel alloc]init];
    
    //    ,              ,    ID
    rectModel.absoluteRect = rect;
    rectModel.muiID = [NSString stringWithFormat:@"%ld",index];
    return rectModel;
}
//  MuiID     view,view rectModel    
- (UIView *)scrollView:(TMMuiLazyScrollView *)scrollView itemByMuiID:(NSString *)muiID
{
    //Find view that is reuseable first.
    //       Item
    LazyScrollViewCustomView *label = (LazyScrollViewCustomView *)[scrollView dequeueReusableItemWithIdentifier:@"testView"];
    
    //      ,        
    NSInteger index = [muiID integerValue];
    NSLog(@"========%ld",index);
    //        
    if (!label)
    {
        label = [[LazyScrollViewCustomView alloc]initWithFrame:[(NSValue *)[rectArray objectAtIndex:index] CGRectValue]];
        label.textAlignment = NSTextAlignmentCenter;
        label.reuseIdentifier = @"testView"; //  ID
    }
    
    label.frame = [(NSValue *)[rectArray objectAtIndex:index]CGRectValue];
    label.text = [NSString stringWithFormat:@"%lu",(unsigned long)index];
    label.backgroundColor = [self randomColor];
    label.userInteractionEnabled = YES;
    
    [scrollView addSubview:label];

    [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)]];
    
    return label;
}

PS:GitHubで提供されているこのDemoは、効率的な多重化をより多く示しています.対応する需要シーンが少ないためか、霊性が欠けていると感じ、後期には実践を試みる必要があります.