【问题标题】:Scroll To Item UICollectionView with UIButton programmatically以编程方式使用 UIButton 滚动到项目 UICollectionView
【发布时间】:2019-10-04 17:16:37
【问题描述】:

我有片刻的困惑,我无法解决一件非常简单的事情。

我的 CollectionView 有 50 个单元格,我想使用“下一步”按钮和“返回”按钮在单元格之间来回滚动。我知道 scrollToItemAtIndexPath 方法,但我无法找到正确的 IndexPath .. 有人可以帮助我吗?

这是我的代码:

// My personal method

-(void)scrollYearAtIndex:(NSInteger)index {
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    [_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

// Show Next Items
-(void)didScrollNextYear {
    [self scrollYearAtIndex:????];
}

// Show Previous Items
-(void)didScrollPreviousYear {
    [self scrollYearAtIndex:????];
}

【问题讨论】:

  • 你的下一个/上一个逻辑是否可以在当前可见的单元格或当前选定的单元格上工作?
  • 所有可见单元格:)

标签: ios objective-c uicollectionview uibutton nsindexpath


【解决方案1】:

将此函数调用到下一个和上一个按钮IBAction

 func scrollToNextCell()
{

    let collectionView = yourCollectionView
    let cellSize = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
    let contentOffset = collectionView!.contentOffset;
    collectionView!.scrollRectToVisible(CGRect(x: contentOffset.x + cellSize.width, y: contentOffset.y, width: cellSize.width, height: cellSize.height), animated: true)
}

func scrollToPreviousCell()
{

    let collectionView = yourCollectionView
    let cellSize = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
    let contentOffset = collectionView!.contentOffset;
    collectionView!.scrollRectToVisible(CGRect(x: contentOffset.x - cellSize.width, y: contentOffset.y, width: cellSize.width, height: cellSize.height), animated: true)
}

【讨论】:

    【解决方案2】:

    我已经在Swift 中给出了答案。你可以在Objective-C中写同样的逻辑。

    要获取下一个 indexPath,请获取已排序的 indexPathsForVisibleItems 数组的 last indexPath 并递增 1。

    要获取前一个indexPath,获取排序后的indexPathsForVisibleItems数组中的firstindexPath并减1。

    func didScrollNextYear() {
        if let index = self.collectionView.indexPathsForVisibleItems.sorted().last?.row {
            let nextIndex = index+1
            if nextIndex < self.collectionView.numberOfItems(inSection: 0) {
                self.collectionView.scrollToItem(at: IndexPath(row: nextIndex, section: 0), at: .centeredHorizontally, animated: true)
            }
        }
    }
    
    func didScrollPreviousYear() {
        if let index = self.collectionView.indexPathsForVisibleItems.sorted().first?.row {
            let previousIndex = index-1
            if previousIndex >= 0 {
                self.collectionView.scrollToItem(at: IndexPath(row: previousIndex, section: 0), at: .centeredHorizontally, animated: true)
            }
        }
    }
    

    【讨论】:

    • NSIndexPath *indexPath = [[self.collectionView indexPathsForVisibleItems] firstObject];在 ObjC 中是正确的??如果让索引 = self.collectionView.indexPathsForVisibleItems.sorted()。第一个?.row
    • indexPathsForVisibleItems进行排序并按升序排序。您还需要firstObject 上一个和lastObject 下一个。
    • 我正在尝试这种方式,但我仍然崩溃..... NSArray *indexPaths = [_collectionView indexPathsForVisibleItems]; NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"row" 升序:YES]; NSArray *orderedIndexPaths = [[indexPaths sortedArrayUsingDescriptors:@[sort]] lastObject];
    【解决方案3】:
    let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()
    let indexPath : NSIndexPath = indexPaths[0] as NSIndexPath
    

    这将为您提供当前选择的索引路径。在didScrollNextYear 方法中,将 indexPath.row 加一。在didScrollPreviousYear 中,将 indexPath.row 减一。

    示例代码:

    // Show Next Items
    -(void)didScrollNextYear {
        let indexPaths : NSArray = self.collectionView!.indexPathsForSelectedItems()
        let indexPath : NSIndexPath = indexPaths[0] as NSIndexPath
    
        [self scrollYearAtIndex: indexPath.row+1];
    }
    

    【讨论】:

    • 非常感谢您的建议(也许我误解了自己).. 这样我只能从所选单元格开始在上一个和下一个之间导航.. 我需要用户滚动浏览所有单元格 Next / Back 不考虑选定的单元格
    • @kAiN: 你可以通过 let index = self.collectionView.indexPathsForVisibleItems.sorted().last?.row 获得可见单元格的索引。理解逻辑,只需要获取索引,其余的可以根据业务情况递增/递减
    猜你喜欢
    • 2019-01-31
    • 2021-03-11
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2017-09-14
    相关资源
    最近更新 更多