线程 1:EXC_BAD_ACCESS(代码=2,地址=0x16f913820)
Thread 1: EXC_BAD_ACCESS (code=2, address=0x16f913820)
我正在尝试以编程方式将 collectionView 添加到 subview.But,当我 return 的项目数为 1 时,集合正在加载如此多的 cells.Here 我在我的中使用 SharkORM项目,它会影响 collectionView 吗?我的实际要求是我将有文件,图像集合需要存储在相关 file.but 这个集合视图没有加载所需数量的 cells.Can 谁能帮助解决这个。
func createCollection(){
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
flowLayout.itemSize = CGSize(width: 82, height: 82)
flowLayout.minimumInteritemSpacing = 0
flowLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
collectionView = UICollectionView(frame: CGRect(x: 10, y: 0, width: self.customView.frame.size.width-20, height: self.customView.frame.size.height), collectionViewLayout: flowLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "ReviewCollectionCellPage", bundle: nil), forCellWithReuseIdentifier: "ReviewCollectionCellPage")
collectionView.backgroundColor = UIColor.clear
customView.addSubview(collectionView)
}
extension ReviewViewController:UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(finalImages.count)
return finalImages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// print(finalImages)
let cell : ReviewCollectionCellPage = collectionView.dequeueReusableCell(withReuseIdentifier: "ReviewCollectionCellPage", for: indexPath) as! ReviewCollectionCellPage
cell.finalImage.image = UIImage(named: "images.png")
collectionView.reloadData()
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 50, height: 50)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
}
代码进入死循环。
在cellForRow
中调用reloadData
调用cellForRow
调用reloadData
调用cellForRow
调用reloadData
调用cellForRow
调用 reloadData
调用 cellForRow
调用 reloadData
调用 cellForRow
调用 reloadData
调用 cellForRow
... 溢出异常
删除 cellForRow
中的 collectionView.reloadData()
并在填充数据源数组后立即重新加载数据。
我正在尝试以编程方式将 collectionView 添加到 subview.But,当我 return 的项目数为 1 时,集合正在加载如此多的 cells.Here 我在我的中使用 SharkORM项目,它会影响 collectionView 吗?我的实际要求是我将有文件,图像集合需要存储在相关 file.but 这个集合视图没有加载所需数量的 cells.Can 谁能帮助解决这个。
func createCollection(){
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
flowLayout.itemSize = CGSize(width: 82, height: 82)
flowLayout.minimumInteritemSpacing = 0
flowLayout.scrollDirection = UICollectionView.ScrollDirection.vertical
collectionView = UICollectionView(frame: CGRect(x: 10, y: 0, width: self.customView.frame.size.width-20, height: self.customView.frame.size.height), collectionViewLayout: flowLayout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(UINib(nibName: "ReviewCollectionCellPage", bundle: nil), forCellWithReuseIdentifier: "ReviewCollectionCellPage")
collectionView.backgroundColor = UIColor.clear
customView.addSubview(collectionView)
}
extension ReviewViewController:UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(finalImages.count)
return finalImages.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// print(finalImages)
let cell : ReviewCollectionCellPage = collectionView.dequeueReusableCell(withReuseIdentifier: "ReviewCollectionCellPage", for: indexPath) as! ReviewCollectionCellPage
cell.finalImage.image = UIImage(named: "images.png")
collectionView.reloadData()
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 50, height: 50)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
}
代码进入死循环。
在cellForRow
中调用reloadData
调用cellForRow
调用reloadData
调用cellForRow
调用reloadData
调用cellForRow
调用 reloadData
调用 cellForRow
调用 reloadData
调用 cellForRow
调用 reloadData
调用 cellForRow
... 溢出异常
删除 cellForRow
中的 collectionView.reloadData()
并在填充数据源数组后立即重新加载数据。