修復(fù)UICollectionView reloadData 閃屏

解決方法就是 不直接調(diào)用 reloadData,需要判斷 item 是 刪除 or 增加,進(jìn)行 deleteItemsAtIndexPathsinsertItemsAtIndexPaths 。然后對(duì)于沒(méi)有變化的那一部分,做 reloadItemsAtIndexPaths。

#import <UIKit/UIKit.h>

@interface UICollectionView (DDReload)


/**
 判斷數(shù)量有沒(méi)有變化,有變化,則 insert or delete 之后,reloadIndexPath,沒(méi)有變化,則 reloadIndexPath。
 */
- (void)ddxq_reloadData;

@end
#import "UICollectionView+DDReload.h"

@implementation UICollectionView (DDReload)


- (void)ddxq_reloadData
{
    NSUInteger sections = [self numberOfSections];
    NSUInteger newSections = sections;
    if ([self.dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
        newSections = [self.dataSource numberOfSectionsInCollectionView:self];
    }
    NSInteger sectionsOffset = newSections - sections;
    
    // new indexPaths after reload data
    NSMutableArray *newIndexPaths = [[NSMutableArray alloc] init];
    
    // need to add
    NSMutableArray *addIndexPaths = [[NSMutableArray alloc] init];
    // need to delete
    NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
   
 NSRange addSectionsRange = NSMakeRange(0, 0),deleteSectionsRange = NSMakeRange(0, 0);
    // 1. find section count changes
    if (sectionsOffset > 0) { // add
        addSectionsRange = NSMakeRange(sections, labs(sectionsOffset));
        for (NSInteger i = sections; i < newSections; i++) {
            // new section
            NSUInteger newCount = [self.dataSource collectionView:self numberOfItemsInSection:i];
            NSArray *paths = [self createIndexPathInSection:i range:NSMakeRange(0, newCount)];
            [addIndexPaths addObjectsFromArray:paths];
        }
    } else if (sectionsOffset < 0){ // delete
        deleteSectionsRange = NSMakeRange(newSections, labs(sectionsOffset));
        for (NSInteger i = newSections; i < sections; i++) {
            NSUInteger newCount = [self.dataSource collectionView:self numberOfItemsInSection:i];
            NSArray *paths = [self createIndexPathInSection:i range:NSMakeRange(0, newCount)];
            [deleteIndexPaths addObjectsFromArray:paths];
        }
    }
    
    // add changed items in every section not changed
    NSUInteger sameSection = sectionsOffset > 0 ? sections : newSections;
    for (NSInteger i = 0; i < sameSection; i++) {
        [self changedItemInSection:i newIndexPaths:newIndexPaths addIndexPaths:addIndexPaths deletePaths:deleteIndexPaths];
    }
    
    @try {
        [self performBatchUpdates:^{
            if (addSectionsRange.length > 0) {
                [self insertSections:[NSIndexSet indexSetWithIndexesInRange:addSectionsRange]];
            }
            if (addIndexPaths.count > 0) {
                [self insertItemsAtIndexPaths:addIndexPaths];
            }
            if (deleteSectionsRange.length > 0) {
                [self deleteSections:[NSIndexSet indexSetWithIndexesInRange:deleteSectionsRange]];
            }
            if (deleteIndexPaths.count > 0){
                [self deleteItemsAtIndexPaths:deleteIndexPaths];
            }
        } completion:^(BOOL finished) {
        }];
    } @catch (NSException *exception) {
        NSLog(@"Error updating collection view: %@", exception);
    }

    // reload sections the image will also be flashed
    // 1.method
//    [UIView animateWithDuration:0.25 animations:^{
//        if (newIndexPaths.count>0) {
//            [self reloadItemsAtIndexPaths:newIndexPaths];
////            [self reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newSections)]];
//        }
//    }];
    // 2.method
    @try {
        [self performBatchUpdates:^{
            if (newIndexPaths.count>0) {
                  [self reloadItemsAtIndexPaths:newIndexPaths];
//                [self reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, newSections)]];
            }
        } completion:^(BOOL finished) {
        }];
    } @catch (NSException *exception) {
        NSLog(@"Error updating collection view: %@", exception);
    }
}

- (NSMutableArray <NSIndexPath *>*)createIndexPathInSection:(NSUInteger)section range:(NSRange)range
{
    NSMutableArray *newIndexPaths = [[NSMutableArray alloc] init];
    for (NSInteger i = range.location; i < range.length; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:section];
        [newIndexPaths addObject:indexPath];
    }
    return newIndexPaths;
}


- (void)changedItemInSection:(NSUInteger)section
               newIndexPaths:(NSMutableArray *)newIndexPaths
               addIndexPaths:(NSMutableArray *)addIndexPaths
                 deletePaths:(NSMutableArray *)deleteIndexPaths
{
    NSUInteger count = [self numberOfItemsInSection:section];
    NSUInteger newCount = [self.dataSource collectionView:self numberOfItemsInSection:section];
    
    //        NSMutableArray *addIndexPaths = [[NSMutableArray alloc] init];
    //        NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init];
    
    NSInteger offset = newCount - count;
    if (offset > 0) {
        for (NSInteger i = count; i < newCount; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:section];
            [addIndexPaths addObject:indexPath];
        }
    } else if (offset < 0){
        for (NSInteger i = newCount; i < count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:section];
            [deleteIndexPaths addObject:indexPath];
        }
    }
    
    //        NSMutableArray *newIndexPaths = [[NSMutableArray alloc] init];
    for (int i = 0; i < newCount; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:section];
        [newIndexPaths addObject:indexPath];
    }
}

@end

參考文章:https://techblog.badoo.com/blog/2015/10/08/batch-updates-for-uitableview-and-uicollectionview/

https://github.com/badoo/ios-collection-batch-updates

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 10點(diǎn)半睡,6點(diǎn)半起,2+6.5=8.5小時(shí)有核心技術(shù)的人,才能活到最后。 iOS 閱讀整理在sourcetree...
    士夢(mèng)閱讀 3,156評(píng)論 0 18
  • 記錄一個(gè)值得紀(jì)念的時(shí)刻:今天是2019年9月21日,我的粉絲總數(shù)100人。這兩天,總有網(wǎng)友關(guān)注,我都有點(diǎn)又驚喜又忐...
    自由行走的女人花閱讀 103評(píng)論 0 2
  • 線程是? 線程是操作系統(tǒng)能夠進(jìn)行運(yùn)算調(diào)度的最小單位。它被包含在進(jìn)程之中,是進(jìn)程中的實(shí)際運(yùn)作單位。一條線程指的是進(jìn)程...
    james_chang閱讀 627評(píng)論 0 2
  • 西山 纜車(chē),至少半小時(shí),成石甬道,石壁冰涼光滑,石級(jí)陡峭,俯瞰滇池。送被子結(jié)果送了5、6個(gè)一次性紙杯
    香蕉與芒果閱讀 121評(píng)論 0 1
  • ?淚珠里的珠花【627】★ ?王紅娟 秋風(fēng)習(xí)習(xí) 蕩在溫嶺的小路上 珠花朵朵 搖起秋池淚珠連連 苦澀的音律 奏起 路...
    向前的冰山來(lái)客15669閱讀 486評(píng)論 1 8

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