如何在willDisplayCell
中检测UICollectionView最后一个可见对象?在UITableView中,我可以使用以下代码来完成: -
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
//DO SOMETHING HERE
}
}
然后对于 UICollectionView
,我该怎么做?
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
{
//HOW CAN I DO THIS SAME LIKE TABLEVIEW?
}
}
答案 0 :(得分:1)
indexPathsForVisibleItems
的问题在于 - 根据https://developer.apple.com/documentation/uikit/uicollectionview/1618020-indexpathsforvisibleitems?language=objc - 它包含
未排序的NSIndexPath对象数组
因此,您必须使用不同的方法:
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == collectionView.numberOfSections - 1 && indexPath.row == [collectionView numberOfItemsInSection:collectionView.numberOfSections - 1] - 1) {
// last row
} else {
// "regular" row
}
}
答案 1 :(得分:1)
完全一样: -
-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if([indexPath row] == ((NSIndexPath*)[[collectionView indexPathsForVisibleItems] lastObject]).row){
//DO SOMETHING HERE
}
}
答案 2 :(得分:0)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.collection_NewCandidates visibleCells]) {
NSIndexPath *indexPath = [self.collection_NewCandidates indexPathForCell:cell];
NSLog(@"your cell index %ld",(long)indexPath.item);
}
}