TableView 和 CollectionView数据刷新闪一下以及虚线不显示的问题以及合理使用@autoreleasepool

最近检查代码 发现了两个问题 记录一下~

解决CollectionView reloadData或者reloadSections时的刷新的闪烁问题

将你原来的reloadData   reloadSections像这样包一下:

[UIView performWithoutAnimation:^{
            [self.mainCol performBatchUpdates:^{
                [self.mainCol reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.mainCol.numberOfSections)]];
                [self.mainCol reloadEmptyDataSet];
            } completion:nil];
        }];

最近使用虚线的时候 最开始发现虚线加载不出来,后来仔细想了想,可能是因为要添加虚线的UIView都还没有渲染完毕,所以才没加出来,果然实验了一下就是这样,切记~

  • 虚线
/**
 在制定的view上添加虚线

 @param lineView 需要添加虚线的view
 @param lineLength 虚线长度
 @param lineSpacing 虚线与虚线之间的间隔
 @param lineColor 虚线的颜色
 */
- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽,线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 0, 0); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0); [shapeLayer setPath:path]; CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];

}
还是这样使用:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self drawDashLine:self.alpaLineOne lineLength:4 lineSpacing:1 lineColor:[UIColor blackColor]];
        [self drawDashLine:self.alpaLineTwo lineLength:4 lineSpacing:1 lineColor:[UIColor blackColor]];
    });

合理使用@autoreleasepool

@autoreleasepool {
        NSUInteger userCount = 100;
        for (NSUInteger i = 0; i < userCount; i ++) {
            @autoreleasepool {
                //执行相应的逻辑
                //两层autoreleasepool的好处在于:
                //内层的auto可以保证每次循环结束后清理一次内存,从而减少内存需求
            }
        }
    }

你可能感兴趣的:(TableView 和 CollectionView数据刷新闪一下以及虚线不显示的问题以及合理使用@autoreleasepool)