开发者社区> 问答> 正文

如何从项目的集合视图访问选定的单元格?//项目存储的防火墙

我连接我的快速应用程序与防火墙,现在我试图从收集视图中获取选定项目的项目键,以存储选定的项目到购物车。[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

展开
收起
游客5akardh5cojhg 2019-12-05 21:25:48 342 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
《云防火墙实现多账号统一管控》 立即下载
《用管控策略设定多账号组织全局访问边界》 立即下载
动态、高效,蚂蚁动态卡片的内核逻辑 立即下载

相关实验场景

更多