TableView 和 CollectionView數(shù)據(jù)刷新閃一下以及虛線不顯示的問(wèn)題以及合理使用@autoreleasepool

最近檢查代碼 發(fā)現(xiàn)了兩個(gè)問(wèn)題 記錄一下~

解決CollectionView reloadData或者reloadSections時(shí)的刷新的閃爍問(wèn)題

將你原來(lái)的reloadData   reloadSections像這樣包一下:

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

最近使用虛線的時(shí)候 最開(kāi)始發(fā)現(xiàn)虛線加載不出來(lái),后來(lái)仔細(xì)想了想,可能是因?yàn)橐砑犹摼€的UIView都還沒(méi)有渲染完畢,所以才沒(méi)加出來(lái),果然實(shí)驗(yàn)了一下就是這樣,切記~

  • 虛線
/**
 在制定的view上添加虛線

 @param lineView 需要添加虛線的view
 @param lineLength 虛線長(zhǎng)度
 @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];
    //  設(shè)置虛線顏色為
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  設(shè)置虛線寬度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  設(shè)置線寬,線間距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  設(shè)置路徑
    CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, NULL, 0, 0); CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0); [shapeLayer setPath:path]; CGPathRelease(path);
    //  把繪制好的虛線添加上來(lái)
    [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 {
                //執(zhí)行相應(yīng)的邏輯
                //兩層autoreleasepool的好處在于:
                //內(nèi)層的auto可以保證每次循環(huán)結(jié)束后清理一次內(nèi)存,從而減少內(nèi)存需求
            }
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在蔡家院子后頭有一大塊野草地,以前可能是亂葬崗,因?yàn)橛泻芏嘈?,而且偶爾?huì)見(jiàn)到一些骨頭。 那天可能是春天 ,雜草非...
    藤木同學(xué)閱讀 196評(píng)論 0 0

友情鏈接更多精彩內(nèi)容