swift - 在coolectionView的数组中添加值的问题 - Swift
问题描述
我从数据库中提取数据。我正在根据 CATEGORIDAVTOPLAM.count 的数量分离我获取的数据。例如,第一节有 4 个数据,第二节有 3 个数据。但是,虽然 2.section 中的数据应该是新 DavFile 数组中的 5.6.7 项,但 Array 中应该是 1.2.7。吸引下一个数据。如何通过在单个目录中的部分中计数来添加数据?当我这样做时,我总共有7个数据如下。Array中第一节的排名是1.2.3.4,第二节的排名是1.2.7。价值观。
func getIndexForArray(with indexPath:IndexPath)->Int{
var itemIndex = 0
if indexPath.row != 0{
for _ in 0..<indexPath.section{
itemIndex += (sonsuzCollec.numberOfItems(inSection: indexPath.section) - 1)
}
return (itemIndex + indexPath.row)
}else{
return indexPath.row
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Int(KATEGORIDAVTOPLAM[section]) ?? 0 }
func numberOfSections(in collectionView: UICollectionView) -> Int {
return yeniDavKATIsımNew.count }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellSonsuz", for: indexPath) as! sonsuzCell
let currentIndex = getIndexForArray(with: indexPath)
if currentIndex < yeniDavFile.count{
let urlNew = URL(string: yeniDavFile[currentIndex])
cell.davetiyeFoto.sd_setImage(with: urlNew)
return cell
}else{
return UICollectionViewCell()
}
@objc func kategoriSaydırNew(){
...
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.yeniDavFile.append(s)
} }
DispatchQueue.main.async { self.sonsuzCollec.reloadData() }}
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["TOPLAM"] as? Int {
let s = String(describing: soru_baslik)
self.KATEGORIDAVTOPLAM.append(s)
} }
DispatchQueue.main.async {
self.sonsuzCollec.reloadData()
}
解决方案
推荐阅读
- reactjs - 如何在反应表中使用复选框?
- javascript - 如何将我的数组转换为对象或 json webscraping
- c# - ASP.NET Core MVC 从 cshtml 页面调用异步方法
- mono - 在 Linux 中加载 Mono 库时如何处理未定义的符号
- dynamics-crm - 如何在不引用特定实体的情况下调用 Dynamics Web api 绑定的自定义操作?
- kotlin - 为什么 Gradle 会报告意外的 Kotlin 版本?
- c++ - 类成员变量的隐式默认值?
- python - 如何“平滑”3D 条形图?
- c++ - 如果它创建的另一个进程仍在运行,EGLFS 应用程序不会释放屏幕
- java - Json 字符串在 Socket PHP 和 Java 上损坏