在我的情况
下,我创建一个带有类的自定义单元格的集合视图这是我的自定义 CollectionView 类代码
import UIKit
class Auction_CollectionCell: UICollectionViewCell {
@IBOutlet weak var Productimage: UIImageView!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var bid: UILabel!
@IBOutlet weak var progress: UIProgressView!
@IBOutlet weak var day: UILabel!
@IBOutlet weak var hours: UILabel!
@IBOutlet weak var mins: UILabel!
@IBOutlet weak var secs: UILabel!
@IBOutlet weak var lblday: UILabel!
@IBOutlet weak var lblhours: UILabel!
@IBOutlet weak var lblmin: UILabel!
@IBOutlet weak var lblsecs: UILabel!
@IBOutlet weak var MainView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
通过 CTRL+Drag 将故事板中的所有视图连接到自定义类,@IBOutlet 将被创建。
我这样称呼我的自定义课程
self.AuctionList = NSArray with NSDictionary Content
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.AuctionList.count
}
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : Auction_CollectionCell = (collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as? Auction_CollectionCell)!
cell.lblday.text = "Day"
cell.lblhours.text = "Hours"
cell.lblmin.text = "Minutes"
cell.lblsecs.text = "Secs"
cell.MainView.layer.cornerRadius = 5
cell.MainView.backgroundColor = UIColor.white
cell.MainView.layer.borderWidth = 0.5
cell.MainView.layer.borderColor = UIColor.gray.cgColor
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var width = CGFloat(0)
var height = CGFloat(0)
if (UIDevice.current.userInterfaceIdiom == .pad){
width = CGFloat((self.view.bounds.size.width / 4.0) - 15)
height = CGFloat(246.0)
}else{
width = CGFloat((self.view.bounds.size.width / 2.0) - 15)
height = CGFloat(246.0)
}
print("Resize Image \(width) \(height)")
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let dic = AuctionList[indexPath.row] as? NSDictionary{
Product.product_id = "\((dic["product_id"])!)"
Product.product_image = "\((dic["image"])!)"
Product.auction_id = "\((dic["auction_id"])!)"
performSegue(withIdentifier: "auctionProductDetails", sender: self)
}
}
玩得开心,祝你好运。随意编辑我的答案