Texture
Texture copied to clipboard
Modify the code in the CustomCollectionView Demo, hoping to achieve the preload function. But runtime has been call willBeginBatchFetchWithContext method to add new data
Hello, I modified the code in the CustomCollectionView Demo here, hoping to achieve the preload function. But running will keep call - (void)collectionNode:(ASCollectionNode *)collectionNode willBeginBatchFetchWithContext:(ASBatchContext *)context method to add new data, only when CollectionNode sliding will stop calling, trouble to help have a look.
#import "ViewController.h"
#import <AsyncDisplayKit/AsyncDisplayKit.h>
#import <AsyncDisplayKit/ASCollectionNode+Beta.h>
#import "MosaicCollectionLayoutDelegate.h"
#import "ImageCellNode.h"
#import "ImageCollectionViewCell.h"
#import "NetworkImageNode.h"
// This option demonstrates that raw UIKit cells can still be used alongside native ASCellNodes.
static BOOL kShowUICollectionViewCells = YES;
static NSString *kReuseIdentifier = @"ImageCollectionViewCell";
static NSUInteger kNumberOfImages = 14;
@interface ViewController () <ASCollectionDataSourceInterop, ASCollectionDelegate, ASCollectionViewLayoutInspecting>
{
ASCollectionNode *_collectionNode;
}
@property (nonatomic, strong) NSMutableArray *list;
@end
@implementation ViewController
#pragma mark -
#pragma mark UIViewController
- (instancetype)init
{
MosaicCollectionLayoutDelegate *layoutDelegate = [[MosaicCollectionLayoutDelegate alloc] initWithNumberOfColumns:2 headerHeight:0];
_collectionNode = [[ASCollectionNode alloc] initWithLayoutDelegate:layoutDelegate layoutFacilitator:nil];
_collectionNode.dataSource = self;
_collectionNode.delegate = self;
_collectionNode.leadingScreensForBatching = 2;
// _collectionNode.layoutInspector = self;
if (!(self = [super initWithNode:_collectionNode]))
return nil;
self.list = [NSMutableArray array];
for (NSUInteger idx = 0; idx < kNumberOfImages; idx++) {
NSString *name = [NSString stringWithFormat:@"image_%lu.jpg", (unsigned long)idx];
[self.list addObject:[UIImage imageNamed:name]];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
#pragma mark - ASCollectionNode data source.
- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath {
UIImage *image = self.list[indexPath.item];
return ^{
return [[ImageCellNode alloc] initWithImage:image];
};
}
- (NSInteger)collectionNode:(ASCollectionNode *)collectionNode numberOfItemsInSection:(NSInteger)section {
return self.list.count;
}
/// 接收到一个消息,说明tableView的数据集接近尾声,如果需要,应该获取更多的数据。
- (void)collectionNode:(ASCollectionNode *)collectionNode willBeginBatchFetchWithContext:(ASBatchContext *)context {
NSLog(@"willBeginBatchFetchWithContext");
NSMutableArray *newArr = [NSMutableArray array];
for (NSUInteger idx = 0; idx < kNumberOfImages; idx++) {
NSString *name = [NSString stringWithFormat:@"image_%lu.jpg", (unsigned long)idx];
[newArr addObject:[UIImage imageNamed:name]];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self insertNewRowsInTableView:newArr];
if (context) {
[context completeBatchFetching:YES];
}
});
}
- (void)insertNewRowsInTableView:(NSArray *)newAnimals {
NSInteger oldCount = self.list.count;
[self.list addObjectsFromArray:newAnimals];
[self insertRowWithStart:oldCount NewCount:self.list.count];
}
/// 插入 Row
- (void)insertRowWithStart:(NSInteger)start NewCount:(NSInteger)count{
NSInteger section = 0;
NSMutableArray *indexPaths = [NSMutableArray array];
for (NSUInteger row = start; row < count; row++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:section];
[indexPaths addObject:path];
}
[_collectionNode insertItemsAtIndexPaths:indexPaths];
}
@end