空集合视图Swift

穆罕默德·埃卢米(Mohamed Elloumi)

我遵循了1个教程,并且能够用一些数据(imageview和文本)填充collectionView:

let appleProducts = ["A", "B", "C", "D"]
let imageArray = [UIImage(named: "pug1"), UIImage(named: "pug2"), UIImage(named: "pug3"), UIImage(named: "pug4")]
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{

    return appleProducts.count
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell

    cell.imageView?.image = self.imageArray[indexPath.row]

    cell.title?.text = self.appleProducts[indexPath.row]
    return cell

}

现在从演示项目传递到我的演示,我想用从FirebaseDatabse获得的数据(图片和文本)填充此CollectionView,因此创建了以下方法:

struct item {
let pictureId: String!
let picture: String!
}
var items = [item]()
   func getLatestAddedItems(){
    self.items.removeAll()
    let databaseRef = FIRDatabase.database().reference()
    databaseRef.child("Items").observe(.value, with: {
        snapshot in

        //self.items.insert(item(picture: picture), at: 0)
        for childSnap in snapshot.children.allObjects {

            let snap = childSnap as! FIRDataSnapshot
            //print(snap.key)

            let picture = (snap.value as? NSDictionary)?["bookImage"] as? String ?? ""
            //print(picture)
            self.items.append(item(pictureId:snap.key, picture:picture))

        }
        print(self.items.count)


    })
}

然后,我创建此按钮来调用GetLatestAddedItems方法:

@IBAction func selectAction(_ sender: AnyObject) {
    getLatestAddedItems()
        }

而这一检查结果:

@IBAction func gettableAction(_ sender: AnyObject) {

    print(self.items[0].picture)
    print(self.items[1].picture)
    print(self.items[2].picture)
    print(self.items.count)    }

输出结果:

picture 1 link
picture 2 link
picture 3 link
3

现在,在ContentView方法中进行了必要的更改之后,一切看起来都很好并且正确无误:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{

    return items.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! CollectionViewCell

    //cell.imageView?.image = self.imageArray[indexPath.row]
     let url = NSURL(string: items[indexPath.row].picture)
     let data = NSData(contentsOf: url! as URL)
     cell.imageView?.image = UIImage(data: data! as Data)

    cell.title?.text = self.items[indexPath.row].pictureId
    return cell

}

现在,我得到一个空的ContentView,第一次使用该按钮时它会起作用,因为我调用了getLatestAddedItems()来获取数据并将其添加到Items表中,我尝试在ViewWillAppear或Viewdidload中都对其进行调用,但没有任何变化。

这就是我认为return items.count返回0的结果,因此什么都不会出现任何建议?

德拉威

将您的collectionView的协议委托初始化移至ViewController生命周期范围之一,例如,viewDidLoad()或者viewWillAppear(_animated : Bool)如果您使用的是自定义viewController(即,将collectionView嵌入到viewController中)

并在您的用户每次从其数据库接收值时重新加载您的collectionView。

override func viewDidLoad(){
  super.viewDidLoad()

  self.collectionView.dataSource = self 
  self.collectionView.delegate = self 

 }

func getLatestAddedItems(){
 self.items.removeAll()
 let databaseRef = FIRDatabase.database().reference()
 databaseRef.child("Items").observe(.childAdded, with: {
    snapshot in

    for childSnap in snapshot.children.allObjects {

        let snap = childSnap as! FIRDataSnapshot
        let picture = (snap.value as? NSDictionary)?["bookImage"] as? String ?? ""
         self.items.append(item(pictureId:snap.key, picture:picture))
         print(self.items.count)
         self.collectionView.reloadData() 
     }
   })
}

PS: -所有你的火力服务器调用是异步的,这需要一些时间来检索您的FBDB数据,所以就把print(self.items.count)应该是内部completionBlock的火力点观察通话否则,如果它放在外面会前甚至称您数据已从FBDB中检索到。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

区分WebParam中的空集合和空集合

来自分类Dev

LINQ 到空集合

来自分类Dev

Groups API 空集合

来自分类Dev

从方法返回空集合

来自分类Dev

使用Gson从JSON删除空集合

来自分类Dev

Automapper空集合变为空

来自分类Dev

从方法返回空集合

来自分类Dev

“java.lang.UnsupportedOperationException:空集合”

来自分类Dev

使NA的NA的空集合/集合的总和而不是0?

来自分类Dev

JAXB将空列表映射到空集合?

来自分类Dev

如何合并.Max()以查询LINQ to EF中的空集合?

来自分类Dev

NavigationProperty.GetDependentProperties()返回一个空集合

来自分类Dev

空集合的联接结果是什么

来自分类Dev

实体DbSet和Any()在空集合上产生true

来自分类Dev

.First不会在空集合上引发异常

来自分类Dev

使用gong-wpf放入空集合

来自分类Dev

在空集合上未创建电机索引

来自分类Dev

C#Linq-获取不为空集合的对象

来自分类Dev

MongoDB | 如何仅显示非空集合?

来自分类Dev

使用Mapster映射时,获取空集合而不是null

来自分类Dev

.First不会在空集合上引发异常

来自分类Dev

NavigationProperty.GetDependentProperties()返回一个空集合

来自分类Dev

从转储还原后,MongoDB仍显示空集合

来自分类Dev

在空集合上未创建电机索引

来自分类Dev

HttpServletRequest#getParts()jsp始终返回空集合

来自分类Dev

MVC QueryString是一个空集合

来自分类Dev

为什么查询返回空集合?

来自分类Dev

Laravel 数据库查询返回空集合

来自分类Dev

laravel 5.4 我的查询返回空集合