我一直在学习如何使用UICollection视图。我创建了一个新的单元控制器,并在main.storyboard中将集合视图单元格标识为“UICollectionView”。每当我轻触UICollectionView控制器(通过TapBar控制器)时,我都会看到黑屏。我目前正在使用Firebase导入/导出数据。谢谢你的帮助!
class UserCollectionViewController: UICollectionViewController {
var databaseRef = FIRDatabase.database().reference()
var usersDict = NSDictionary?()
var userNameArray = [String]()
var userImageArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.databaseRef.child("user_profile").observeEventType(.Value, withBlock :{
(snapshot) in
self.usersDict = snapshot.value as? NSDictionary
for(userId,details) in self.usersDict! {
let img = details.objectForKey("profile_pic_small") as! String
let name = details.objectForKey("name") as! String
let firstName = name.componentsSeparatedByString(" ")[0]
self.userImageArray.append(img)
self.userNameArray.append(firstName)
self.collectionView?.reloadData()
}
})
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.userImageArray.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollectionViewCell
return cell
let imageUrl = NSURL(string: userImageArray[indexPath.row])
let imageData = NSData(contentsOfURL: imageUrl!)
cell.userImage.image = UIImage(data: imageData!)
cell.userName.text = userNameArray[indexPath.row]
return cell
}
}发布于 2016-08-02 04:10:01
在单元中放置一个标签(将其相对于单元的约束设置为0,0,0,0 ),然后向其添加imageView和UIView。另外,别忘了确认委托和数据源。
https://stackoverflow.com/questions/38679939
复制相似问题