使用 ASCollectionNode
一、更换
tableNode = [[ASTableNode alloc] init];
和
tableNode = [[ASCollectionNode alloc] initWithCollectionViewLayout:[UICollectionViewFlowLayout new]];
然后将其添加到 ASViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tableNode.view.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
}
- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath {
return ASSizeRangeMake(
CGSizeMake(0, 0),
CGSizeMake(self.view.frame.size.width - 2*10, CGFLOAT_MAX)
);
}
- (NSInteger)collectionNode:(ASCollectionNode *)collectionNode numberOfItemsInSection:(NSInteger)section {
return [_photoFeed numberOfItemsInFeed];
}
- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoModel *photoModel = [_photoFeed objectAtIndex:indexPath.row];
// this will be executed on a background thread - important to make sure it's thread safe
ASCellNode *(^ASCellNodeBlock)() = ^ASCellNode *() {
PhotoCellNode *cellNode = [[PhotoCellNode alloc] initWithPhotoObject:photoModel];
return cellNode;
};
return ASCellNodeBlock;
}
结果:
