gpt4 book ai didi

ios - 对 DynamoDB 为 Collection View 单元格获取的表数据进行分类

转载 作者:行者123 更新时间:2023-11-30 12:27:52 26 4
gpt4 key购买 nike

我正在开发一个服务应用程序,用户在其中创建一个帖子,其详细信息保存在 dynamoDb 表中。我已获取表中的所有数据,现在我想在 Collection View Controller 中显示数据,以便每个单元格代表单个帖子。现在我不知道如何将每个帖子与该数据分离并将其提供给 Collection View 。我的表字段是: Table_Screenshot

我的代码是:

import UIKit
import AWSDynamoDB

class ProvidingViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var PCV: UICollectionView!
let db = AWSDynamoDBObjectMapper.default()
let scanExpression = AWSDynamoDBScanExpression()
var counter:Int = 0

var imagex = ["UserIcon.png", "chat2.png","UserIcon.png", "delete.png","UserIcon.png", "delete.png","UserIcon.png", "delete.png","UserIcon.png", "delete.png","UserIcon.png", "delete.png"]

var images:[String] = []

override func viewDidLoad() {
super.viewDidLoad()
scanner()

}
///
func scanner(){

scanExpression.limit = 2000

db.scan(PostDetails.self, expression: scanExpression).continueWith(block: { (task:AWSTask!) -> AnyObject! in

if task.result != nil {
let paginatedOutput = task.result!

//use the results
for item in paginatedOutput.items as! [PostDetails] {
self.counter = paginatedOutput.items.count
self.images.append(item.userId!)


}

if ((task.error) != nil) {
print("Error: Could not fetch PostDetails table data")
}
return nil

}

return nil
})

}

///

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = PCV.dequeueReusableCell(withReuseIdentifier: "c", for: indexPath) as! CellsCollectionViewCell
cell.ProvImage.image = UIImage(named: imagex[indexPath.row])
cell.ProvLabel.text = images[indexPath.row]
return cell


}


}

我有图像数组,我正在其中获取数据。当我打印出数组时,它有数据,但是当我将其分配给 Collection View Controller 时,屏幕显示为空,即没有单元格。请帮忙。谢谢

最佳答案

您面临的真正问题是,当 View 加载图像数组为空并且 CollectionView 加载为空时,当您在扫描仪方法中加载数组中的图像时,您没有调用 reloadData for CollectionView 这就是为什么在数据加载到数组后您无法在 CollectionView 中看到任何内容。我正在更新您的扫描仪方法,尝试一下,它会起作用。

func scanner(){

scanExpression.limit = 2000

db.scan(PostDetails.self, expression: scanExpression).continueWith(block: { (task:AWSTask!) -> AnyObject! in

if task.result != nil {
let paginatedOutput = task.result!

//use the results
for item in paginatedOutput.items as! [PostDetails] {
self.counter = paginatedOutput.items.count
self.images.append(item.userId!)


}
//This line is important because it tells collectionview that i have new data so please refresh.
PCV.reloadData()
if ((task.error) != nil) {
print("Error: Could not fetch PostDetails table data")
}
return nil

}

return nil
})

}

关于ios - 对 DynamoDB 为 Collection View 单元格获取的表数据进行分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43949992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com