我在数组中有200个项目,我正在传递到集合视图在设备上运行时,由于内存警告,它会崩溃。有没有办法根据滚动将单元格限制在索引路径的行。由于下载图像,它显示内存警告。如果我跳过下载部分,那么它就不会崩溃。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell:ProductListCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductListCell", for: 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)!
cell?.layer.shouldRasterize = true
cell?.layer.rasterizationScale = UIScreen.main.scale;
}
if let url = NSURL(string: ((self.objCategorywiseResponse.SimilirProductsList![indexPath.row].productPic)!)) {
cell?.imgProduct.sd_setImage(with: url as URL, placeholderImage: UIImage(named: "placeholder.png"))
}
if let strProductName = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].ProductName {
cell?.lblProductName.text = strProductName
}
cell?.lblProductDesc .text = ""
if let strProductPrice = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].MRP {
cell?.lblProductPrice.attributedText = Common().setPrice(Price:strProductPrice, StrikePrice1:(self.objCategorywiseResponse.SimilirProductsList![indexPath.row].discountprice)!)
}
if let strDiscount = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].DiscountPercentage {
if strDiscount > 0 {
cell?.btnDiscount.setTitle("\(strDiscount)% off", for: .normal)
} else {
cell?.btnDiscount.isHidden = true
}
}
if let strRatingDesc = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].TotalRating_Reviews {
cell?.lblRatingdesc.text = strRatingDesc
}
if let strRating = self.objCategorywiseResponse.SimilirProductsList![indexPath.row].AvgRatingCount {
cell?.lblRating.text = "\(strRating)"
}
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!
}
答案 0 :(得分:2)
我不认为细胞数量是个问题。 UICollectionView自动管理单元格集合,包括处理屏幕外单元格。
我认为您更有可能遇到内存泄漏,这会阻止在UICollectionView释放它们时实际取消分配单元格。你有很多块可能导致内存泄漏。
答案 1 :(得分:1)
您可以使用分页概念一次停止加载所有单元格。它取消了可重用单元的高内存使用率。
检查这个link将详细说明使用分页概念。
答案 2 :(得分:0)
您没有正确地重用作为内存警告原因的单元格 删除这个`
if (cell == nil) {
let nib: NSArray = Bundle.main.loadNibNamed("ProductListCell", owner: self, options: nil)! as NSArray
cell = (nib.object(at: 0) as? ProductListCell)!
}
` 在viewDidLoad中使用以下代码
self.collectionView.register(UINib.init(nibName: "ProductListCell", bundle: nil), forCellWithReuseIdentifier: "ProductListCell")
如果问题仍然存在,请使用以下库进行图像缓存 Kingfisher
它是一个内存影响较小的轻量级库
答案 3 :(得分:0)
我在ScrollView中放置了Collection View这就是为什么它一次调用所有100个单元格。然后我删除了滚动视图然后它工作正常