[英]Load more functionality for collectionview inside tableview cell
I want to know how can I put load more functionality in collection view which is inside tableview cell.我想知道如何在 tableview 单元格内的集合视图中加载更多功能。 Since collection view scrolling is not enabled.
由于未启用集合视图滚动。
Putting a code of last index checking in collection view cell generating weird behaviour as some time reaching last index doesn't call functional at all and some time it load mores when moving to other cell.将最后一个索引检查的代码放在集合视图单元格中会产生奇怪的行为,因为有时到达最后一个索引根本不会调用功能,而有时它会在移动到其他单元格时加载更多内容。 I know this is happening due to tableview cell.
我知道这是由于 tableview 单元格而发生的。 Can any one help me around how to fix it.
任何人都可以帮助我解决如何解决它。
This is the code I am using:这是我正在使用的代码:
//
// JustForYouTableViewCell.swift
// ShoppingPortal
//
// Created by Faraz Haider on 10/07/2019.
// Copyright © 2019 B2b. All rights reserved.
//
import UIKit
protocol JustForYouTableViewCellDelegate: class {
func justForYouClickedWithItem(_ itemIndex:Int)
func loadMoreDataForJustForYouWithPageNumber(pageNumber : Int, records: Int)
}
class JustForYouTableViewCell: BaseTableViewCell {
@IBOutlet weak var justForYouCollectionView: UICollectionView!
weak var delegate: JustForYouTableViewCellDelegate?
var justForYouArray = [Product]()
var cellHeight:CGFloat = 0
var pageNumber = 1
var currentRecordsCount = 0
var totalNumberOfRecord = 0
var isLoadMore = false
var isReload = false
@IBOutlet weak var collectionHeight: NSLayoutConstraint!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
override func updateCell(rowModel: BaseRowModel) {
if !isReload{
justForYouArray = rowModel.rowValue as! [Product]
pageNumber = rowModel.tag
totalNumberOfRecord = rowModel.rowId
}
currentRecordsCount = justForYouArray.count
if(currentRecordsCount < totalNumberOfRecord){
isLoadMore = true
}else{
isLoadMore = false
}
self.delegate = rowModel.delegate as? JustForYouTableViewCellDelegate
justForYouCollectionView.dataSource = self
justForYouCollectionView.delegate = self
justForYouCollectionView.isScrollEnabled = false
cellHeight = rowModel.rowHeight
NotificationCenter.default.addObserver(self, selector: #selector(doThisWhenNotify(notification:)), name: NSNotification.Name(rawValue: "post"), object: nil)
self.collectionHeight.constant = self.justForYouCollectionView.collectionViewLayout.collectionViewContentSize.height;
justForYouCollectionView.reloadData()
}
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
self.layoutIfNeeded()
let contentSize = self.justForYouCollectionView.collectionViewLayout.collectionViewContentSize
return CGSize(width: contentSize.width, height: contentSize.height + 20) // 20 is the margin of the collectinview with top and bottom
}
@objc func doThisWhenNotify(notification : NSNotification) {
if let info = notification.userInfo as? NSDictionary{
if let id = info["product"] as? [Product]{
justForYouArray.append(contentsOf:id)
}
isReload = true
let homeVC = self.viewController as? HomeVC
homeVC?.dashboardTblView.reloadData()
}
}
@IBAction func moreButtonClicked(_ sender: Any) {
let viewController:MoreProductsVC = UIStoryboard(name: "HomeModule", bundle: nil).instantiateViewController(withIdentifier: "MoreProductsVC") as! MoreProductsVC
viewController.selectedState = .SelectedStateJustForYou
self.viewController?.navigationController?.pushViewController(viewController, animated: true)
}
}
extension JustForYouTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return justForYouArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : JustForYouCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "JustForYouCollectionViewCell", for: indexPath) as! JustForYouCollectionViewCell
let deals = justForYouArray[indexPath.row]
cell.titleLabel.text = Utility.checkEmptyString(deals.name)
if deals.productImage.count>0{
if let imageUrl = deals.productImage[0].url{
let url = URL(string: imageUrl)
let image = UIImage(named: "placeholder")
cell.dealsImageView.kf.setImage(with: url, placeholder: image)
}
}
if deals.setPriceOption == 1{
cell.priceLabel.text = String(format: "%@ %d - %d %@", Utility.checkEmptyString(deals.fobPriceName),deals.min,deals.max,Utility.checkEmptyString(deals.tradeUnitName))
}else{
if deals.productDifferencePriceQuantity.count>0{
cell.priceLabel.text = String(format: "%@ %d - %d %@", "USD",deals.productDifferencePriceQuantity[0].mOQ,deals.productDifferencePriceQuantity[0].fOBPrice,Utility.checkEmptyString(deals.tradeUnitTypeName))
}else{
cell.priceLabel.text = ""
}
}
// Check if the last row number is the same as the last current data element
if indexPath.row == self.justForYouArray.count - 1 && self.isLoadMore {
updateNextSet()
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row)
let deals = justForYouArray[indexPath.row]
let storyBoard: UIStoryboard = UIStoryboard(name: "HomeModule", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ProductDetailVC") as! ProductDetailVC
if (deals.productId != nil){
newViewController.selectedProductId = deals.productId
self.viewController?.navigationController?.pushViewController(newViewController, animated: true)
}
}
func updateNextSet(){
pageNumber += 1
self.delegate?.loadMoreDataForJustForYouWithPageNumber(pageNumber: pageNumber, records: currentRecordsCount)
isLoadMore = false
}
}
extension JustForYouTableViewCell: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let spacing : CGFloat = (collectionViewLayout as? UICollectionViewFlowLayout)?.minimumInteritemSpacing ?? 0.0
let widthPerItem = (collectionView.frame.height - spacing * 2) / 3
return CGSize(width: screenWidth/2, height: 200)
}
}
Ok I did get an answer from another source 😀 for people looking for the solution, what's I did is I put scroll view delegate scrollview did end dragging and in that I put condition of once you reach scroll view max height - height of my last cell I did load more over their.好的,我确实从另一个来源 😀 得到了一个答案,供寻找解决方案的人使用,我所做的是我将滚动视图委托 scrollview 确实结束拖动,并且我将条件设置为一旦你达到滚动视图最大高度 - 我最后一个单元格的高度我确实在他们身上加载了更多。 Thanks everyone
谢谢大家
** **
** **
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
// UITableView only moves in one direction, y axis
let currentOffset = scrollView.contentOffset.y
let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
// Change 10.0 to adjust the distance from bottom
if maximumOffset - currentOffset <= 235 {
if (self.isLoadMore){
self.isFirstTime = false
justForYouPageNumber += 1
callServiceFoNewArrivals(withPageNumber: justForYouPageNumber, andRecords: 10)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.