我连接我的快速应用程序与防火墙,现在我试图从收集视图中获取选定项目的项目键,以存储选定的项目到购物车。[Firebase`import UIKit导入Firebase导入Firebase数据库导入SDWebImage类ViewController 6:UIViewController,UICollectionViewDataSource,UICollectionViewDelegate{
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var itemCollectionView: UICollectionView!
var customItemFlowLayout: CustomItemFlowLayout!
var items = [itemsL]()
//var ind = 0
var dbRef: DatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
nextButton.layer.cornerRadius = 10
dbRef = Database.database().reference().child("items")
loadDB()
customItemFlowLayout = CustomItemFlowLayout()
itemCollectionView.collectionViewLayout = customItemFlowLayout
//itemCollectionView.backgroundColor = .white
}
func loadDB(){
dbRef.observe(DataEventType.value, with: { (snapshot) in
var newItems = [itemsL]()
for itemsLSnapshot in snapshot.children {
// condition for filtring items
if itemsL.init(snapshot: itemsLSnapshot as! DataSnapshot).key >= "2"{
// bring item
// #######
let itemsLObject = itemsL(snapshot: itemsLSnapshot as! DataSnapshot)
newItems.append(itemsLObject)
// #######
}
}
self.items = newItems
self.itemCollectionView.reloadData()
})
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt IndexPath: IndexPath) -> UICollectionViewCell {
let cell = itemCollectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: IndexPath) as! ItemCollectionViewCell
let item = items[IndexPath.row]
cell.imageView.sd_setImage(with: URL(string: item.url),placeholderImage: UIImage(named: "image22"))
cell.itemName.text = item.itemName
cell.price.text = item.price
cell.layer.borderColor = UIColor.gray.cgColor
cell.layer.borderWidth = 1
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderColor = UIColor.blue.cgColor
cell?.layer.borderWidth = 3
cell?.isSelected = true
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderColor = UIColor.gray.cgColor
cell?.layer.borderWidth = 1
cell?.isSelected = false
}
}`]1
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。