簡體   English   中英

在CollectionView中獲取可見的細胞索引路徑

[英]Get Visible Cell Index Path in CollectionView

我有一個垂直UICollectionView我有在頂部和底部的兩個指標UICollectionView 我有15個牢房。 但是當前只有10個可見。 如果單元格0不可見,我想顯示頂部垂直指示器,表明頂部有一些單元格。 如果單元格零可見,則頂部不需要指示器

同樣,如果當前時間不顯示“底部”中的單元格,則希望具有“底部指示器”。

我為此創建了一個示例視圖控制器。 希望對您有幫助。 我添加了兩個按鈕作為指示器(頂部和底部)。 希望對您有幫助。

在此處輸入圖片說明

這就是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,以使其易於理解。 您可以使用此腳本或情節提要。 歡呼,祝你有美好的一天。

 [![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

然后,您可以推斷出自己的邏輯以顯示想要顯示的內容

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM