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

时间:2019-04-16 05:47:45

标签: ios swift uicollectionview

我有一个垂直的UICollectionView,在UICollectionView的顶部和底部有两个指示器。 我有15个牢房。但是当前只有10个可见。如果单元格0不可见,我想显示顶部垂直指示器,表明顶部有一些单元格。如果显示零单元格,则顶部不需要指示符

同样,如果当前时间底部的单元格不可见,我想使用底部指示器。

3 个答案:

答案 0 :(得分:1)

我为此创建了一个示例视图控制器。希望对您有帮助。我添加了两个按钮作为指示器(顶部和底部)。希望对您有帮助。

enter image description here

这是ViewController的外观

  

代码:

//
//  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
                }
            }
        }
    }
}

我已经通过编程方式创建了UI,以使其易于理解。您可以使用此脚本或情节提要。欢呼,祝你有美好的一天。

答案 1 :(得分:0)

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

enter image description here

 @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)
    }

答案 2 :(得分:0)

let arrayOfVisibleItems = collectionView.indexPathsForVisibleItems.sorted()
let lastIndexPath = arrayOfVisibleItems.last
let firstIndexPath = arrayOfVisibleItems.first

然后您可以推断出自己的逻辑以显示想要显示的内容