其实我有像这样的 UICollectionView 可见单元格
- (void)scrollViewDidEndDeceleratingUIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.obj_CollectionView1 visibleCells]) {
NSIndexPath *indexPath = [self.obj_CollectionView1 indexPathForCell:cell];
lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
if (lastIndex ==9) {
[self.rightButton setHidden:YES];
}
if (lastIndex==0) {
[self.leftButton setHidden:YES];
}
if (lastIndex>3) {
[self.leftButton setHidden:NO];
}
if (lastIndex<6) {
[self.rightButton setHidden:NO];
}
}
}
现在通过使用该索引路径,我想左右滚动,但我很困惑在 newIndexPath 中写什么
-(IBAction)RightButtonid)sender
{
if (lastIndex <4 && lastIndex >9)
{
[_obj_CollectionView1 scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
}
同样适用于左键
请帮忙
\
答案:
NSUInteger lastIndex;
NSIndexPath *indexPathOf;
NSMutableArray *indexContainer;
NSInteger IndexVisibleCell,IndexVisible;
NSArray *labelcontent;
NSArray *reversedArray;
-(void)awakeFromNib
{
_obj_CollectionView1.delegate = self;
_obj_CollectionView1.dataSource = self;
indexContainer = [[NSMutableArray alloc]initWithCapacity:15];
labelcontent = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
}
-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
{
InnerCollectionCell1 *innerCell= [collectionView dequeueReusableCellWithReuseIdentifier"innerCell1" forIndexPath:indexPath];
innerCell.innerViewCell1.layer.borderColor = [UIColor brownColor].CGColor;
innerCell.innerViewCell1.layer.borderWidth = 1;
innerCell.innerLabelCell1.text = [labelcontent objectAtIndex:indexPath.item];
return innerCell;
}
-(NSInteger)collectionViewUICollectionView *)collectionView numberOfItemsInSectionNSInteger)section
{
return [labelcontent count];
}
- (CGSize)collectionViewUICollectionView *)collectionView layoutUICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPathNSIndexPath *)indexPath
{
CGFloat Width = _obj_CollectionView1.frame.size.width-20;
float cellWidth =Width / 3.0;
CGSize size = CGSizeMake(cellWidth,_obj_CollectionView1.frame.size.height );
return size;
}
-(IBAction)RightButtonid)sender
{
NSMutableArray *pat = [[ _obj_CollectionView1 indexPathsForVisibleItems] mutableCopy];
[pat sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSInteger r1 = [obj1 row];
NSInteger r2 = [obj2 row];
if (r1 > r2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (r1 < r2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSIndexPath *pathh = [NSIndexPath indexPathForRow:[(NSIndexPath *)[pat lastObject]item ]+1 inSection:0];
[self.obj_CollectionView1 scrollToItemAtIndexPath:pathh atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}
-(IBAction)LeftButton:(id)sender
{
NSMutableArray *pat = [[ _obj_CollectionView1 indexPathsForVisibleItems] mutableCopy];
[pat sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSInteger r1 = [obj1 row];
NSInteger r2 = [obj2 row];
if (r1 > r2) {
return (NSComparisonResult)NSOrderedDescending;
}
if (r1 < r2) {
return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}];
NSIndexPath *pathh = [NSIndexPath indexPathForRow:[(NSIndexPath *)[pat firstObject]item ]-1 inSection:0];
[self.obj_CollectionView1 scrollToItemAtIndexPath:pathh atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
IndexVisible = [indexPath indexAtPosition:[indexPath length] - 1];
if (IndexVisible==labelcontent.count-1)
{
[self.rightButton setHidden:YES];
}
if (IndexVisible<[labelcontent count]-3)
{
[self.rightButton setHidden:NO];
}
if (IndexVisible >2) {
[self.leftButton setHidden:NO];
}
if (IndexVisible<1) {
[self.leftButton setHidden:YES];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.obj_CollectionView1 visibleCells]) {
indexPathOf = [self.obj_CollectionView1 indexPathForCell:cell];
lastIndex = [indexPathOf indexAtPosition:[indexPathOf length] - 1];
if (lastIndex==labelcontent.count-1)
{
[self.rightButton setHidden:YES];
}
if (lastIndex<[labelcontent count]-3)
{
[self.rightButton setHidden:NO];
}
if (lastIndex >2) {
[self.leftButton setHidden:NO];
}
if (lastIndex<1) {
[self.leftButton setHidden:YES];
}
}}
Best Answer-推荐答案 strong>
你的意思是说,一次最多应该有 3 个项目可见,对吧?
如果是这样,请创建 UICollectionViewFlowLayout 属性,以便最多 3 个可见
- (void)calculateCollectionLayout
{
[self.collectionView setContentInset:UIEdgeInsetsMake(kMargin, kMargin, kMargin, kMargin)];
UICollectionViewFlowLayout *layout = ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout);
[layout setMinimumInteritemSpacing:kPadding];
[layout setMinimumLineSpacing:kMargin];
CGFloat cellWidth = ((CGRectGetWidth(self.view.frame) - (kNumberOfColumns + 1) * kPadding) - (2 * kMargin)) / kNumberOfItemsToBeVisible ;
[layout setItemSize:CGSizeMake(cellWidth,cellHeight)];
[layout invalidateLayout];
}
在上面的代码中 kNumberOfItemsToBeVisible 将为 3
并根据项目总数显示/隐藏左右按钮跟踪currentLoadedItems
例如 currentPage 将是 3 并且 left 最初将被隐藏(如果它是 <= 3 隐藏左右按钮)。单击 Right 时,将其增加 3。并检查它是否 >= totalItems 。如果是,则隐藏右键。如果 currentpage 是 1 再次隐藏左键
关于ios - 在单击左右按钮时将 CollectionView 的单元格向左和向右滚动,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/37804642/
|