ios UICollectionView 当前可见单元格索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18649920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionView current visible cell index
提问by Irfan DANISH
I am using UICollectionView
first time in my iPad application.
I have set UICollectionView
such that its size and cell size is same, means only once cell is displayed at a time.
我UICollectionView
第一次在我的 iPad 应用程序中使用。我已经设置UICollectionView
了它的大小和单元格大小相同,这意味着一次只显示一次单元格。
Problem:Now when user scroll UICollectionView I need to know which cell is visible I have to update other UI elements on change. I didn't find any delegate method for this. How can I achieve this?
问题:现在当用户滚动 UICollectionView 时,我需要知道哪个单元格可见,我必须在更改时更新其他 UI 元素。我没有为此找到任何委托方法。我怎样才能做到这一点?
Code:
代码:
[self.mainImageCollection setTag:MAIN_IMAGE_COLLECTION_VIEW];
[self.mainImageCollection registerClass:[InspirationMainImageCollectionCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.mainImageFlowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.mainImageFlowLayout setMinimumInteritemSpacing:0.0f];
[self.mainImageFlowLayout setMinimumLineSpacing:0.0f];
self.mainImageFlowLayout.minimumLineSpacing = 0;
[self.mainImageCollection setPagingEnabled:YES];
[self.mainImageCollection setShowsHorizontalScrollIndicator:NO];
[self.mainImageCollection setCollectionViewLayout:self.mainImageFlowLayout];
What I have tried:
我尝试过的:
As UICollectionView
Conforms to UIScrollView
, I got when user scroll ends with UIScrollViewDelegate
method
由于UICollectionView
符合UIScrollView
,当用户滚动结束时我得到了UIScrollViewDelegate
方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
But inside above function how can I get current visible cell index of UICollectionView
???
但是在上面的函数中,我怎样才能获得UICollectionView
??? 的当前可见单元格索引?
回答by Anthony
indexPathsForVisibleItems
might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:
indexPathsForVisibleItems
可能适用于大多数情况,但有时它会返回一个包含多个索引路径的数组,并且找出您想要的索引路径可能会很棘手。在这些情况下,您可以执行以下操作:
CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
This works especially well when each item in your collection view takes up the whole screen.
当集合视图中的每个项目都占据整个屏幕时,这尤其有效。
Swift version
迅捷版
let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)
回答by LE SANG
The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get
[collectionView visibleCells] 方法为您提供了您想要的所有可见细胞数组。当你想得到时使用它
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
NSLog(@"%@",indexPath);
}
}
Update to Swift 5:
更新到 Swift 5:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in yourCollectionView.visibleCells {
let indexPath = yourCollectionView.indexPath(for: cell)
print(indexPath)
}
}
回答by Sam Bing
Swift 5:
斯威夫特5:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()
visibleRect.origin = collectionView.contentOffset
visibleRect.size = collectionView.bounds.size
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }
print(indexPath)
}
Working Answers Combined In Swift 2.2 :
Swift 2.2 中结合的工作答案:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
var visibleRect = CGRect()
visibleRect.origin = self.collectionView.contentOffset
visibleRect.size = self.collectionView.bounds.size
let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
guard let indexPath = visibleIndexPath else { return }
print(indexPath)
}
回答by Vincil Bishop
For completeness sake, this is the method that ended up working for me. It was a combination of @Anthony & @iAn's methods.
为了完整起见,这是最终为我工作的方法。这是@Anthony 和@iAn 方法的组合。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
NSLog(@"%@",visibleIndexPath);
}
回答by Mr Stanev
It will probably be best to use UICollectionViewDelegate methods: (Swift 3)
最好使用 UICollectionViewDelegate 方法:(Swift 3)
// Called before the cell is displayed
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print(indexPath.row)
}
// Called when the cell is displayed
func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
print(indexPath.row)
}
回答by user1105951
Just want to add for others : for some reason, I didnt not get the cell that was visible to the user when I was scrolling to previouscell in collectionView with pagingEnabled.
只想为其他人添加:出于某种原因,当我使用 pagingEnabled滚动到collectionView 中的上一个单元格时,我没有得到用户可见的单元格。
So I insert the code inside dispatch_async to give it some "air" and this works for me.
所以我在 dispatch_async 中插入代码以给它一些“空气”,这对我有用。
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
dispatch_async(dispatch_get_main_queue(), ^{
UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];
[visibleCell doSomthing];
});
}
回答by Hiren Panchal
For Swift 3.0
对于 Swift 3.0
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let visibleRect = CGRect(origin: colView.contentOffset, size: colView.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let indexPath = colView.indexPathForItem(at: visiblePoint)
}
回答by Anil Kukadeja
Swift 3.0
斯威夫特 3.0
Simplest solution which will give you indexPath for visible cells..
最简单的解决方案,它将为您提供可见单元格的 indexPath ..
yourCollectionView.indexPathsForVisibleItems
will return the array of indexpath.
将返回索引路径数组。
Just take the first object from array like this.
像这样从数组中取出第一个对象。
yourCollectionView.indexPathsForVisibleItems.first
I guess it should work fine with Objective - C as well.
我想它也应该适用于 Objective-C。
回答by Mihail Salari
UICollectionView current visible cell index: Swift 3
UICollectionView 当前可见单元格索引:Swift 3
var visibleCurrentCellIndexPath: IndexPath? {
for cell in self.collectionView.visibleCells {
let indexPath = self.collectionView.indexPath(for: cell)
return indexPath
}
return nil
}
回答by Mihail Salari
converting @Anthony's answer to Swift 3.0worked perfectly for me:
将@Anthony 的答案转换为Swift 3.0对我来说非常有效:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
var visibleRect = CGRect()
visibleRect.origin = yourCollectionView.contentOffset
visibleRect.size = yourCollectionView.bounds.size
let visiblePoint = CGPoint(x: CGFloat(visibleRect.midX), y: CGFloat(visibleRect.midY))
let visibleIndexPath: IndexPath? = yourCollectionView.indexPathForItem(at: visiblePoint)
print("Visible cell's index is : \(visibleIndexPath?.row)!")
}