我将从数据库中提取的数据添加到CollectionView。我将添加为数组的数据放入模型数组中。我在collectionView中看到了Array中的数据。有时数据平滑添加,但有时却出现错误
“线程1:致命错误:索引超出范围”
。有时在工作时有时为什么不呢?我认为collectionView.reloadData()有问题。
@IBOutlet weak var sonsuzCollec: UICollectionView!
var model = [[String]]()
var davetiyefilee = [String]()
var davetiyefilee2 = [String]()
extension denemeView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
if (collectionView == sonsuzCollec) {
return model[section].count
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
if (collectionView == sonsuzCollec) {
return yeniDavKATIsımNew.count
}
return 0
}
...
}
@objc func davetiyeCEK1() {
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["davetiyefilee"] as? String {
let s = String(describing: soru_baslik)
self.davetiyefilee.append(s)
}
}
self.model.append(self.davetiyefilee)
DispatchQueue.main.async { [weak self] in
self?.sonsuzCollec?.reloadData()
}
}
}
@objc func davetiyeCEK2() {
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["davetiyefilee"] as? String {
let s = String(describing: soru_baslik)
self.davetiyefilee2.append(s)
}
}
self.model.append(self.davetiyefilee2)
DispatchQueue.main.async { [weak self] in
self?.sonsuzCollec?.reloadData()
}
}
}
答案 0 :(得分:1)
我认为这是因为模型数组的section项目为空。 您正在使用多少个收藏夹?您可以显示更多完整代码吗?
或者您的编号中有另一个方法可以尝试
if (collectionView == sonsuzCollec) {
var numberofRows = 0
if model[section].count > 0 {
numberofRows = model[section].count
} else {
numberofRows = 0
}
return numberofRows
}