Ios 在tableview单元格内为collectionview加载更多功能
我想知道如何在tableview单元格内的集合视图中加载更多功能。因为集合视图滚动未启用 在集合视图单元格中放入最后一个索引检查代码会产生奇怪的行为,因为到达最后一个索引的某段时间根本不调用函数,而在移动到其他单元格时,它会加载更多的函数。我知道这是由于tableview单元格造成的。谁能帮我修理一下吗 这是我正在使用的代码:Ios 在tableview单元格内为collectionview加载更多功能,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我想知道如何在tableview单元格内的集合视图中加载更多功能。因为集合视图滚动未启用 在集合视图单元格中放入最后一个索引检查代码会产生奇怪的行为,因为到达最后一个索引的某段时间根本不调用函数,而在移动到其他单元格时,它会加载更多的函数。我知道这是由于tableview单元格造成的。谁能帮我修理一下吗 这是我正在使用的代码: // // JustForYouTableViewCell.swift // ShoppingPortal // // Created by Faraz Haide
//
// 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)
}
}
//
//JustForYouTableViewCell.swift
//购物门户
//
//由Faraz Haider于2019年7月10日创建。
//版权所有©2019 B2b。版权所有。
//
导入UIKit
仅适用于YouTableViewCellDelegate的协议:类{
func justForYouClickedWithItem(itemIndex:Int)
func loadMoreDataForJustForYouWithPageNumber(页码:Int,记录:Int)
}
仅为YouTableViewCell类:BaseTableViewCell{
@IBO仅适用于YouCollectionView:UICollectionView!
弱var委托:仅适用于YouTableViewCellDelegate?
var justForYouArray=[Product]()
变量cellHeight:CGFloat=0
变量pageNumber=1
var currentRecordsCount=0
var totalNumberOfRecord=0
var isLoadMore=false
var isReload=false
@IBOutlet弱var集合高度:NSLayoutConstraint!
重写func awakeFromNib(){
super.awakeFromNib()
//初始化代码
}
覆盖功能设置选定(uSelected:Bool,动画:Bool){
super.setSelected(选定,动画:动画)
//为所选状态配置视图
}
重写func updateCell(行模型:BaseRowModel){
如果!以色列{
justForYouArray=rowModel.rowValue as![Product]
pageNumber=rowModel.tag
totalNumberOfRecord=rowModel.rowId
}
currentRecordsCount=justForYouArray.count
if(currentRecordsCountCGSize{
self.layoutifneed()
让contentSize=self.justForYouCollectionView.collectionViewLayout.collectionViewContentSize
返回CGSize(width:contentSize.width,height:contentSize.height+20)//20是CollectionView的页边距,包括顶部和底部
}
@objc func doThisWhenNotify(通知:NSNotification){
如果let info=notification.userInfo as?NSDictionary{
如果让id=info[“产品”]作为?[产品]{
justForYouArray.append(contentsOf:id)
}
isReload=true
将homeVC=self.viewController设为?homeVC
homeVC?.dashboardTblView.reloadData()
}
}
@iAction func moreButtonClicked(\发送方:任意){
让viewController:MoreProductsVC=UIStoryboard(名称:“HomeModule”,捆绑包:nil)。实例化viewController(标识符为:“MoreProductsVC”)为!MoreProductsVC
viewController.selectedState=.SelectedStateJustForYou
self.viewController?.navigationController?.pushViewController(viewController,动画:true)
}
}
仅适用于YouTableViewCell的扩展名:UICollectionViewDataSource,UICollectionViewDelegate{
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
返回justForYouArray.count
}
func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
将单元格:JustForYouCollectionViewCell=collectionView.dequeueReusableCell(带有ReuseIdentifier:“JustForYouCollectionViewCell”,for:indexPath)设为!JustForYouCollectionViewCell
let deals=justForYouArray[indexPath.row]
cell.titleLabel.text=Utility.checkEmptyString(deals.name)
如果deals.productImage.count>0{
如果让imageUrl=deals.productImage[0].url{
让url=url(字符串:imageUrl)
让image=UIImage(名为“占位符”)
cell.dealsImageView.kf.setImage(带:url,占位符:image)
}
}
如果deals.setPriceOption==1{
cell.pricellabel.text=String(格式:“%@%d-%d%@”、Utility.checkEmptyString(deals.fobPriceName)、deals.min、deals.max、Utility.checkEmptyString(deals.tradeUnitName))
}否则{
如果deals.productDifferencePriceQuantity.count>0{
cell.priceLabel.text=String(格式:“%@%d-%d%@”,“USD”,deals.productDifferencePriceQuantity[0]。mOQ,deals.productDifferencePriceQuantity[0]。fOBPrice,Utility.checkEmptyString(deals.tradeUnitTypeName))
}否则{
cell.pricellabel.text=“”
}
}
//检查最后一行号是否与最后一个当前数据元素相同
如果indexath.row==self.justForYouArray.count-1&&self.isLoadMore{
updateNextSet()
}
返回单元
}
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
打印(indexPath.row)
let deals=justForYouArray[indexPath.row]
让故事板:UIStoryboard=UIStoryboard(名称