self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.isPrefetchingEnabled = false
self.collectionView.layer.shadowColor = UIColor.black.cgColor
self.collectionView.layer.shadowOffset = CGSize(width: -1, height: 1)//CGSizeMake(0, 1)
self.collectionView.layer.shadowOpacity = 0.5
self.collectionView.layer.shadowRadius = 3.0
self.collectionView.clipsToBounds = false
self.collectionView.layer.masksToBounds = false
这就是我在didLoad中对collection视图所做的
现在的问题是当我设置数据源数组时
var myDataSource:[Custom Model] = []
当我更改myDataSourceArray时,它最初有10个值在更改之后这个数组现在只有1个值当我重新加载集合视图时它给出了这个。。。
*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.54.4/UICollectionViewData.m:435
2018-10-29 12:28:27.884302+0500 MyBetterDealsv2[14203:147469] Task <A3EE0E68-F980-47FD-A5B2-941C1D233A34>.<7> finished with error - code: -1001
2018-10-29 12:28:27.904908+0500 MyBetterDealsv2[14203:145748] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}'
我也尝试过使布局无效,但它给出了同样的错误,但还没有得到解决方案:(
下面是collectionview的委托方法
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if(self.myallDeals != nil && self.myallDeals?.count != 0){
self.collectionViewHeightConstraint.constant = 220
}
return self.myallDeals?.count ?? 0
}
// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dealCell", for: indexPath ) as! DealsCell
let cellData = self.myallDeals?[indexPath.row]
cell.dealTitle.text = cellData?.dealTitle
cell.dealDescription.text = cellData?.dealLocation
cell.discount.text = "$\(cellData?.discountedPrice ?? "") now"
cell.dealDistanceLabel.text = "\(cellData?.distance ?? 0.0) km away"
cell.ratingView.rating = cellData?.averageRating ?? 0.0
cell.dealImage.sd_setImage(with: URL(string: cellData?.dealPicture ?? ""), placeholderImage: nil)
if(cellData?.isliked == true){
cell.likeButton.backgroundColor = UIColor(red: 251/255, green: 106/255, blue: 74/255, alpha: 1.0)
cell.likeButton.setImage(UIImage(named: "liked"), for: .normal)
}else{
cell.likeButton.backgroundColor = UIColor(red:255, green: 255, blue:255, alpha: 1.0)
cell.likeButton.setImage(UIImage(named: "heart"), for: .normal)
}
//===========================
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// handle tap event
if(Constants.isGuestUser){
self.present(Utilities.SelectionButtonActionSheet(), animated: true, completion: nil)
}else{
let deal = self.myallDeals?[indexPath.row]
//dealdetail
let vc = self.storyboard?.instantiateViewController(withIdentifier: "dealdetail") as! DealDetailVC
vc.mydeal = deal
Utilities.animationOnPush(sender: self, nxtVC: vc)
print("You selected cell.")
}
}
这是我用sateesh代码得到的