簡體   English   中英

向自定義 UITableViewCell 添加無限滾動和分頁

[英]Add infinite scroll and pagination to custom UITableViewCell

我有一個自定義的 UITableViewCell,里面有 UICollectionView。 我想在 Cell 中向此 collectionView 添加無限滾動。 但由於未知原因,無限滾動處理程序只被調用一次......

我的手機的偽代碼:

class CahnellsTableViewCell: UITableViewCell {
            
    
    public private(set) var collectionView: UICollectionView?
    class var reuseIdentifier: String { return "EpgTvChannelsTableViewCell"}
    private var channelCell: EpgTvChannelsCollectionViewCell!
    private var tvChannelList: [(PBBTvChannelMediaModel, PBBAccessStatusInfo)]?
            
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepare()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepare() {
        configureCollectionView(flowLayout: configureCollectionViewFlowLayout())
    }
    
    private func configureCollectionView(flowLayout: UICollectionViewFlowLayout) {
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        guard let collectionView = collectionView else { return }
        collectionView.register(EpgTvChannelsCollectionViewCell.self, forCellWithReuseIdentifier: EpgTvChannelsCollectionViewCell.reuseIdentifier)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        contentView.addSubview(collectionView)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        appendSharedConstrainst(constraints: [
            collectionView.topAnchor.constraint(equalTo: topAnchor),
            collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
            collectionView.bottomAnchor.constraint(equalTo: bottomAnchor),
            collectionView.trailingAnchor.constraint(equalTo: trailingAnchor)
        ])
        bindInfiniteScroll()
    }
    
    private func configureCollectionViewFlowLayout() -> UICollectionViewFlowLayout {
        let collectionViewLayout = UICollectionViewFlowLayout()
        collectionViewLayout.scrollDirection = .horizontal
        return collectionViewLayout
    }
    
    private func bindInfiniteScroll() {
        collectionView?.infiniteScrollTriggerOffset = 300
        collectionView?.infiniteScrollIndicatorView = createInfiniteScrollIndicatorView()
        collectionView?.infiniteScrollDirection = .horizontal
        collectionView?.isPagingEnabled = true
        collectionView?.isScrollEnabled = true
        collectionView?.addInfiniteScroll { [weak self] _ in
            print("JJP infinite scroll called!!")
            // function to get more data is called here
        }
        collectionView?.setShouldShowInfiniteScrollHandler { [weak self] _ -> Bool in
            //check if can download MORE
            return true
        }
    }
    
    private func createInfiniteScrollIndicatorView() -> UIView {
        let infiniteScrollIndicatorView = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.large)
        infiniteScrollIndicatorView.color = UIColor.Control.normal
        return infiniteScrollIndicatorView
    }

    func setTvChannelList(data: [(PBBTvChannelMediaModel, PBBAccessStatusInfo)]?) {
        //this fucntion is called when we receive data (appended data from all pagination)
        tvChannelList = data
        collectionView?.reloadData()
    }
}

通常,處理程序中的這段代碼只被調用一次:

collectionView?.addInfiniteScroll { [weak self] _ in
            print("JJP infinite scroll called!!")
            // function to get more data is called here
        }

我刪除了部分代碼以使其更易於閱讀。 你有什么建議為什么上面的代碼塊只被調用一次? 你能給我解釋一下嗎? infiniteScrollIndicatorView 也只顯示一次。

謝謝幫助。

當我們收到 nee 數據時,我們必須添加“collectionView?.finishInfiniteScroll()”,否則不會調用上面的代碼塊。

所以當我在此處添加collectionView?.finishInfiniteScroll()時它會起作用:

func setTvChannelList(data: [(PBBTvChannelMediaModel, PBBAccessStatusInfo)]?) {
        //this fucntion is called when we receive data (appended data from all pagination)
        tvChannelList = data
        collectionView?.finishInfiniteScroll()
        collectionView?.reloadData()
    }

暫無
暫無

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

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