简体   繁体   English

在CollectionView中获取可见的细胞索引路径

[英]Get Visible Cell Index Path in CollectionView

I am having a vertical UICollectionView I am having two indicators in the top and bottom of UICollectionView . 我有一个垂直UICollectionView我有在顶部和底部的两个指标UICollectionView I am having 15 cells. 我有15个牢房。 But only 10 is visible in the current time. 但是当前只有10个可见。 If cell 0 is not visible I want to show top vertical indicator that there are some cells in the top. 如果单元格0不可见,我想显示顶部垂直指示器,表明顶部有一些单元格。 If cell zero is visible No Indicator needed in the top 如果单元格零可见,则顶部不需要指示器

Similarly I want to have the Bottom Indicator if the cells in Bottom is not visible in the current time. 同样,如果当前时间不显示“底部”中的单元格,则希望具有“底部指示器”。

I have create a sample view controller for this. 我为此创建了一个示例视图控制器。 hope this will help to you. 希望对您有帮助。 I added two buttons as indicators( top and bottom). 我添加了两个按钮作为指示器(顶部和底部)。 hope this will help to you. 希望对您有帮助。

在此处输入图片说明

This is how ViewController looks like 这就是ViewController的样子

Code : 代码:

//
//  ViewController.swift
//  Assignment
//
//  Created by Anuradh Caldera on 4/16/19.
//  Copyright © 2019 All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    private var customtableView: UITableView!
    private var cellidentifier = "cellIdentifier"
    private var topButton: UIButton!
    private var bottomButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        setuptableview()
        setupButtons()
    }
}

extension ViewController {
    fileprivate func setuptableview() {
        customtableView = UITableView(frame: .zero, style: .plain)
        customtableView.translatesAutoresizingMaskIntoConstraints = false
        customtableView.register(UITableViewCell.self, forCellReuseIdentifier: cellidentifier)
        customtableView.dataSource = self
        customtableView.delegate = self
        view.addSubview(customtableView)
        let customtableviewConstraints = [customtableView.leftAnchor.constraint(equalTo: view.leftAnchor),
                                          customtableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
                                          customtableView.rightAnchor.constraint(equalTo: view.rightAnchor),
                                          customtableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50)]
        NSLayoutConstraint.activate(customtableviewConstraints)

    }
}

extension ViewController {
    fileprivate func setupButtons() {
        topButton = UIButton()
        topButton.translatesAutoresizingMaskIntoConstraints = false
        topButton.setTitle("Go Top", for: .normal)
        topButton.backgroundColor = .purple
        topButton.setTitleColor(.white, for: .normal)
        topButton.isHidden = true
        view.addSubview(topButton)
        let topbuttonConstraints = [topButton.leftAnchor.constraint(equalTo: view.leftAnchor),
                                    topButton.topAnchor.constraint(equalTo: view.topAnchor),
                                    topButton.rightAnchor.constraint(equalTo: view.rightAnchor),
                                    topButton.heightAnchor.constraint(equalToConstant: 50)]
        NSLayoutConstraint.activate(topbuttonConstraints)

        bottomButton = UIButton()
        bottomButton.translatesAutoresizingMaskIntoConstraints = false
        bottomButton.setTitle("Go Bottom", for: .normal)
        bottomButton.backgroundColor = .purple
        bottomButton.setTitleColor(.white, for: .normal)
        bottomButton.isHidden = false
        view.addSubview(bottomButton)
        let bottomButtonConstraints = [bottomButton.leftAnchor.constraint(equalTo: view.leftAnchor),
                                       bottomButton.bottomAnchor.constraint(equalTo: view.bottomAnchor),
                                       bottomButton.rightAnchor.constraint(equalTo: view.rightAnchor),
                                       bottomButton.heightAnchor.constraint(equalToConstant: 50)]
        NSLayoutConstraint.activate(bottomButtonConstraints)
    }
}

extension ViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 30 // use your desired number of cells here and change other according to the cell count.
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellidentifier)
        cell?.textLabel?.text = "cell \(indexPath.row)"
        return cell!
    }
}


extension ViewController: UITableViewDelegate, UIScrollViewDelegate {

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        topButton.isHidden = true
        bottomButton.isHidden = false
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let visiblerect = CGRect(origin: customtableView.contentOffset, size: customtableView.bounds.size)
        let visiblepoint = CGPoint(x: visiblerect.minX, y: visiblerect.minY)
        if let visibleindexpath = customtableView.indexPathForRow(at: visiblepoint) {
            if visibleindexpath.row == 0 {
                topButton.isHidden = true
            } else {
                topButton.isHidden = false
            }
        }

        for cell in customtableView.visibleCells {
            if let indexpath = customtableView.indexPath(for: cell) {
                print("visible cell from cell : \(indexpath.row)")
                if indexpath.row == 29 {
                    bottomButton.isHidden = true
                } else {
                    bottomButton.isHidden = false
                }
            }
        }
    }
}

I have create the UI programmatically to make it understandable. 我已经以编程方式创建了UI,以使其易于理解。 you can use either this or storyboard. 您可以使用此脚本或情节提要。 cheers and have a nice day. 欢呼,祝你有美好的一天。

 [![you need to take one button inside a UIView][1]][1]

在此处输入图片说明

 @IBOutlet weak var topMoveView: ViewDesign!
 @IBOutlet weak var topMoveButton: ButtonDesign! 

override func viewWillAppear(_ animated: Bool) {

        topMoveView.isHidden = true
        topMoveButton.isHidden = true
    }

extension testViewController : UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if (testCollectionView.contentOffset.y <= 0) {
            topMoveView.isHidden = true
            topMoveButton.isHidden = true
        }else{
            topMoveView.isHidden = false
            topMoveButton.isHidden = false
        }
    }

}


@IBAction func topMoveButtonPressed(_ sender: ButtonDesign) {
        self.testCollectionView.setContentOffset(.zero, animated: true)
    }
let arrayOfVisibleItems = collectionView.indexPathsForVisibleItems.sorted()
let lastIndexPath = arrayOfVisibleItems.last
let firstIndexPath = arrayOfVisibleItems.first

You can then deduce your own logic to show the whatever you want to show 然后,您可以推断出自己的逻辑以显示想要显示的内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当不是可见单元格时获取单元格集合的值 - Get values of cell collectionView when not are visible cells 单元格变量为零,其中设置了值 collectionview 索引路径 - cell variable is nil where value was set collectionview index path Swift 2.1-如何通过collectionView单元格的索引路径行进行筛选 - Swift 2.1 - How to pass index path row of collectionView cell to segue IGListKit:获取当前可见的单元格索引 - IGListKit: get the currently visible cell index 如果当前部分单元格高度为零,则不会为后续部分调用索引路径处的行的 Collectionview 单元格 - Collectionview cell for row at index path not getting called for subsequent sections if the current section cell height is zero 更改 collectionView 单元格下方的标题和描述 当集合视图单元格更改时。 使用索引路径执行此操作不起作用 - Changing the title and the description below the collectionView cell When the collection view cell change. Doing this with index path is not working 如何在tableview单元格内的collectionview单元格的按钮单击时获取索引 - How to get index on button click of collectionview cell which is inside tableview cell 如何获取iOS中的最后一个单元格索引路径? - How to get last cell index path in iOS? Swift CollectionView获取第一个单元格 - Swift CollectionView get first cell 通过segue将CollectionView的选定单元格索引发送到viewController - Send selected cell index of CollectionView to viewController by segue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM