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

时间:2018-01-19 13:59:33

标签: swift3

当我正在进行集合视图重载数据时,方法一次调用100次,因为我的数组有100.但是当我在故事板中使用它调用5次然后我滚动它然后它正在调用。无法在下面提到的xib和代码中使用xib.loading中的故事板。

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()
    }
    }
}

1 个答案:

答案 0 :(得分:1)

UICollectionViewUIScrollView的子类,请尝试使用scrollview方法scrollViewDidEndDecelerating

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()
   }
}