Swift3 如何在xib中查找集合视图重新加载数据问题

Swift3 如何在xib中查找集合视图重新加载数据问题,swift3,Swift3,当我在收集视图中重新加载数据时,下面的方法一次调用100次,因为我的数组有100次。但当我在故事板中使用时,它调用了5次,然后我滚动它,然后它调用。我不想在xib中将其作为故事板使用。从xib加载,代码如下所述 override func viewDidLoad() { super.viewDidLoad() self.clvProducts.register(UINib(nibName: "ProductListCell", bundle: nil), forCellWithRe

当我在收集视图中重新加载数据时,下面的方法一次调用100次,因为我的数组有100次。但当我在故事板中使用时,它调用了5次,然后我滚动它,然后它调用。我不想在xib中将其作为故事板使用。从xib加载,代码如下所述

override func viewDidLoad() {
    super.viewDidLoad()

  self.clvProducts.register(UINib(nibName: "ProductListCell", bundle: nil), forCellWithReuseIdentifier: "ProductListCell")
}

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if collectionView == clvProducts {


        var cell:ProductListCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductListCell", for: indexPath as IndexPath) as? ProductListCell
        if (cell == nil) {
            let nib: NSArray = Bundle.main.loadNibNamed("ProductListCell", owner: self, options: nil)! as NSArray
            cell = (nib.object(at: 0) as! ProductListCell)
            }
        if let url = NSURL(string: ((self.objCategorywiseResponse.SimilirProductsList![indexPath.row].productPic)!)) {
                  //  cell?.imgProduct.sd_setImage(with: url as URL, placeholderImage: UIImage(named: "placeholder.png"))
                    cell?.imgProduct.kf.setImage(with: url as URL)
            }
            if let intRating = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].AvgRatingCount {
                cell?.subViewRating.rating = intRating
            }

            if let strShopName = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].ShopName {
                cell?.lblShopName.text = strShopName
            }

            if let strDisctance = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].Distance {
                cell?.lblDistance.setTitle("\(strDisctance) km near by you", for: .normal)
            }

            if let strLandLineNo = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].LandLineNumber {
                cell?.lblMobileNo.text = "\(strLandLineNo)"
            }

            cell?.btnAddToCompare.tag = indexPath.row
            cell?.btnAddToCompare.addTarget(self, action: #selector(btnClickedAddToCompare(_:)), for: .touchUpInside)

            cell?.btnAddProduct.tag = indexPath.row
            cell?.btnAddProduct.addTarget(self, action: #selector(btnClickedAddProduct(_:)), for: .touchUpInside)

            cell?.btnCall.tag = indexPath.row
             cell?.btnCall.addTarget(self, action: #selector(self.callCellactn(_:)), for: .touchUpInside)

            cell?.btnMap.tag = indexPath.row
             cell?.btnMap.addTarget(self, action: #selector(self.locationCellactn(_:)), for: .touchUpInside)

            return cell!
        }  
}
  func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if collectionView == clvProducts {
    if indexPath.row == ((self.objCategorywiseResponseStage1).count - 1)
    {
        print("came to last row")
      //  self.moreData()
    }
    }
}
UICollectionView是UIScrollView的子类,请尝试使用scrollview方法


试着解释一下你想要的更好如果我滚动集合视图,当它转到最后一个单元格时,我想调用另一个方法。我正在对集合视图进行分页,首先我给出20行,然后到了底部,我将添加一些行calling@Abhiram您定义了委托吗?扩展ProductsListCategoryVC:UIScrollViewDelegate{func scrollViewDidScroll\uScrollView:UIScrollView{let offsetX:CGFloat=scrollView.contentOffset.x;let contentWidth:CGFloat=scrollView.frame.size.width;printoffset x%.0foffsetX}func ScrollViewDiEndDeclaring_scrollView:UIScrollView{let bottom=scrollView.contentOffset.y+scrollView.frame.size.height如果bottom>=scrollView.contentSize.height{PrintComed到最后一行}}collectionview继承自scrollview,在您拥有collectionview方法的类上尝试,不要使用UIScrollViewDelegates在重新加载索引路径的数据单元格时,aarray中调用了所有100个项目,但故事板中的iam它的调用只有5个,当我滚动它时,它正在调用
override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
   let bottom = scrollView.contentOffset.y + scrollView.frame.size.height
   if bottom >= scrollView.contentSize.height {
        print("came to last row")
        //self.moreData()
   }
}