Abhiram
Abhiram

Reputation: 247

how to find collection view reload data issue in xib

While i'm doing collection view reload data below method was calling at a time for 100 times because of my array having 100.but when i am using in story board it call for 5 times then i scroll it then it is calling. ineed to work it as storyboard in xib.loading from xib and code s mentioned below.

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

Upvotes: 0

Views: 69

Answers (1)

Claudio
Claudio

Reputation: 5203

UICollectionView is a subclass of the UIScrollView, try using the scrollview method 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()
   }
}

Upvotes: 1

Related Questions