我在 collectionView 中使用 viewController 和 collectionView 。 1st collectionView 需要制作表格, 2nd collectionView 需要滚动图片。
我可以显示一个图片,但在我的类模型中应该加载更多图片(图片,图片2,图片3 )。
所以我的类型号:
class Model {
var image: String
var image2: String
var image3: String
var images: [String] = []
var images2: [String] = []
var images3: [String] = []
var ref: FIRDatabaseReference!
init(snapshot: FIRDataSnapshot) {
ref = snapshot.ref
let value = snapshot.value as! NSDictionary
let snap = value["hall1"] as? NSDictionary
let snap2 = value["hall2"] as? NSDictionary
let snap3 = value["hall3"] as? NSDictionary
image = snap?["qwerty"] as? String ?? ""
image2 = snap2?["qwerty"] as? String ?? ""
image3 = snap3?["qwerty"] as? String ?? ""
}
}
我的 viewController :
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var image: [Model] = []
var ref: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference(withPath: "Студии2")
ref.observe(.value, with: { (snapshot) in
var newImage: [Model] = []
for imageSnap in snapshot.children {
let imageObj = Model(snapshot: imageSnap as! FIRDataSnapshot)
newImage.append(imageObj)
}
self.image = newImage
self.collectionView.reloadData()
})
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return image.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell11
cell.vc1 = self
cell.imagess = [image[indexPath.item]]
return cell
}
}
我的 collectionCell1 :
class CollectionViewCell11: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
var imagess: [Model] = []
@IBOutlet weak var collectionView: UICollectionView!
var vc1: ViewController?
override func awakeFromNib() {
super.awakeFromNib()
collectionView.delegate = self
collectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return imagess.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell12
cell.imageView.sd_setImage(with: URL(string: imagess[indexPath.item].image))
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if vc1 != nil {
let vc2 = vc1!.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
vc2.photo = [imagess[indexPath.item]]
let backItem = UIBarButtonItem()
backItem.title = ""
vc1!.navigationItem.backBarButtonItem = backItem
vc1!.navigationController?.pushViewController(vc2, animated: true)
}
}
}
我的 collectionCell2 :
class CollectionViewCell12: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
}
在 collectionCell1 中使用字符串是不可能的,因为类模型有一个数组图像,以便将来传递给 viewController
我是新手,但我认为需要在新类中使用另一个类模型。但我可能是错的。 我理解我的错误,我的错误在 collectionCell1 行 -
cell.imageView.sd_setImage(with: URL(string: imagess[indexPath.item].image))
但我不明白我能做什么......
它应该是这样的:
但早些时候我在 var imagess 中使用了 String 类型,现在我需要使用 class Model