• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 在单击左右按钮时将 CollectionView 的单元格向左和向右滚动

[复制链接]
菜鸟教程小白 发表于 2022-12-11 16:43:32 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

其实我有像这样的 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-推荐答案


你的意思是说,一次最多应该有 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap