UICollection현재 표시 가능한 셀 인덱스 보기
하고 .UICollectionView
아이패드팅세세 i i i i i를 했습니다.UICollectionView
셀의 크기와 셀 크기가 동일하도록 합니다. 즉, 셀이 한 번에 한 번만 표시됩니다.
문제:사용자가 스크롤할 때UICollectionView
표시되는 셀을 알아야 하며 변경 시 다른 UI 요소를 업데이트해야 합니다.을 사용하다떻게게 하면 ?? ???
코드:
[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];
내가 시도한 것:
~로UICollectionView
UIScrollView
, 사용자 스크롤이 다음과 같이 종료되었을 때 표시됩니다.UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
하면 셀 수 있을까요?UICollectionView
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];
이 기능은 컬렉션 보기의 각 항목이 전체 화면을 차지할 때 특히 잘 작동합니다.
스위프트 버전
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)
[ collection View visible Cells ]메서드는 원하는 모든 visible Cells 배열을 제공합니다.원할 때 사용하세요.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
NSLog(@"%@",indexPath);
}
}
Swift 5로 업데이트:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in yourCollectionView.visibleCells {
let indexPath = yourCollectionView.indexPath(for: cell)
print(indexPath)
}
}
Swift 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)
}
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)
}
완성도를 높이기 위해, 이것이 나에게 통하게 된 방법입니다.@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);
}
UICollection 현재 표시 가능한 셀 인덱스 보기: Swift 3, 4, 5+
var visibleCurrentCellIndexPath: IndexPath? {
for cell in self.collectionView.visibleCells {
let indexPath = self.collectionView.indexPath(for: cell)
return indexPath
}
return nil
}
확장 기능으로서:
extension UICollectionView {
var visibleCurrentCellIndexPath: IndexPath? {
for cell in self.visibleCells {
let indexPath = self.indexPath(for: cell)
return indexPath
}
return nil
}
}
사용방법:
if let indexPath = collectionView.visibleCurrentCellIndexPath {
/// do something
}
UICollection ViewDelegate 메서드(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)
}
다른 사용자를 위해 추가하고 싶을 뿐입니다.collection 내의 이전 셀로 스크롤할 때 어떤 이유로 사용자에게 표시되는 셀을 얻을 수 없었습니다.paging Enabled를 사용하여 표시했습니다.
그래서 dispatch_async에 코드를 삽입하여 "에어"를 부여하면 됩니다.
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
dispatch_async(dispatch_get_main_queue(), ^{
UICollectionViewCell * visibleCell= [[self.collectionView visibleCells] objectAtIndex:0];
[visibleCell doSomthing];
});
}
Swift 3.0
표시되는 셀의 indexPath를 제공하는 가장 간단한 솔루션.
yourCollectionView.indexPathsForVisibleItems
indexpath 배열을 반환합니다.
이렇게 배열에서 첫 번째 개체를 가져옵니다.
yourCollectionView.indexPathsForVisibleItems.first
Objective - C에서도 잘 될 것 같아요.
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)
}
Swift 3.0에 대한 @Anthony의 답변은 나에게 딱 들어맞았다.
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)!")
}
하시면 됩니다.scrollViewDidEndDecelerating
이 : 이경경
//@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
NSLog(@"visible cell value %d",lastIndex);
}
}
이 스레드에서는 셀이 전체 화면을 표시하지만 Visible rect의 컬렉션 뷰 경계와 중간점을 사용하는 경우 잘 작동하는 솔루션이 매우 많습니다. 그러나 이 문제에 대한 간단한 해결책이 있습니다.
DispatchQueue.main.async {
let visibleCell = self.collImages.visibleCells.first
print(self.collImages.indexPath(for: visibleCell))
}
이를 통해 표시되는 셀의 indexPath를 얻을 수 있습니다.Dispatch Queue를 추가한 이유는 스와이프 속도가 빨라졌을 때 다음 셀이 잠깐 표시되었을 때 dispactch Queue를 사용하지 않으면 화면에 표시되는 셀이 아닌 간단히 표시된 셀의 index Path를 얻을 수 있기 때문입니다.
오래된 질문이지만 제 경우엔...
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.firstObject].row;
}
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
_m_offsetIdx = [m_cv indexPathForCell:m_cv.visibleCells.lastObject].row;
}
이 스니펫도 확인해 주세요.
let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)
이거 먹어봐, 효과가 있어.(예를 들어 아래 예에서는 3개의 셀이 있습니다.)
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let visibleRect = CGRect(origin: self.collectionView.contentOffset, size: self.collectionView.bounds.size)
let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))
let visibleIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)
if let v = visibleIndexPath {
switch v.item {
case 0: setImageDescription()
break
case 1: setImageConditions()
break
case 2: setImageResults()
break
default: break
}
}
Swift 3 및 Swift 4:
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[1])
}
+1을 더하는 것보다 실제 숫자를 표시하려면
언급URL : https://stackoverflow.com/questions/18649920/uicollectionview-current-visible-cell-index
'programing' 카테고리의 다른 글
Git 푸시 오류: 리포지토리를 찾을 수 없습니다. (0) | 2023.04.11 |
---|---|
Windows에서 cURL을 설치하고 사용하는 방법 (0) | 2023.04.11 |
아이폰 잠금 해제 시 X코드 "디바이스 잠금" (0) | 2023.04.11 |
Objective-C 2.0에서 권장되지 않는 메서드에 플래그를 붙이려면 어떻게 해야 합니까? (0) | 2023.04.11 |
npm을 사용하여 bash 스크립트 실행 (0) | 2023.04.11 |