如何使用swift在UICollectionView单元格中加载自定义单元格(xib)
我用Swift创build了一个小样本项目。 我创build了一个“MyCustomView”作为包含标签,button和imageView的xib,如下面的代码所示:
import UIKit @IBDesignable class MyCustomView: UIView { @IBOutlet weak var lblName: UILabel! @IBOutlet weak var btnClick: UIButton! @IBOutlet weak var myImageView: UIImageView! var view:UIView! @IBInspectable var mytitleLabelText: String? { get { return lblName.text } set(mytitleLabelText) { lblName.text = mytitleLabelText } } @IBInspectable var myCustomImage:UIImage? { get { return myImageView.image } set(myCustomImage) { myImageView.image = myCustomImage } } override init(frame : CGRect) { super.init(frame: frame) xibSetup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) xibSetup() } func xibSetup() { view = loadViewFromNib() view.frame = self.bounds // not sure about this ? view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight] addSubview(view) } func loadViewFromNib() -> UIView { let bundle = NSBundle(forClass: self.dynamicType) let nib = UINib(nibName: "MyCustomView", bundle: bundle) let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView return view } }
附上xib的图像供参考。
在StoryBoard – > ViewController中添加了UIViewCollection,如下图所示。 在这个viewcollection中,我需要这个橙色单元格来包含我的自定义xib,以便在运行时加载。
我如何做到这一点?
Sandeepbuild议的新的修改代码
// 1导入UIKit
class ViewController: UIViewController { @IBOutlet weak var collectionView: UICollectionView! override func viewDidLoad() { super.viewDidLoad() self.collectionView.registerNib(UINib(nibName: "MyCustomView", bundle: nil), forCellWithReuseIdentifier: "myCell") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 7 } func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! MyCustomView cell.lblName.text = "MyNewName" return cell } }
// 2导入UIKit
@IBDesignable class MyCustomView: UICollectionViewCell { @IBOutlet weak var lblName: UILabel! @IBOutlet weak var btnClick: UIButton! @IBOutlet weak var myImageView: UIImageView! var view:UIView! @IBInspectable var mytitleLabelText: String? { get { return lblName.text } set(mytitleLabelText) { lblName.text = mytitleLabelText } } @IBInspectable var myCustomImage:UIImage? { get { return myImageView.image } set(myCustomImage) { myImageView.image = myCustomImage } } }
SIA,
这是你可以做的,
-
改变你的
MyCustomView
calss是UICollectionViewCell的子类而不是UIView。 -
删除
override init(frame : CGRect)
,required init?(coder aDecoder: NSCoder)
,func xibSetup()
,func loadViewFromNib() -> UIView
从MyCustomView
-
我真的不明白你如何使用你的setter和getter的mytitleLabelText和myCustomImage。 如果没用的话也把它除掉。
-
最后,你将在MyCustomView中留下IBOutlets。
-
为了更好的编码实践将名称从MyCustomView更改为MyCustomCell(可选)
-
转到您的xib,selectxib并将其类设置为MyCustomView。
- 在同一个屏幕上更改文件所有者到托pipecollectionView的yourView控制器
-
在viewController的ViewDidLoad中注册你的笔尖。
self.collectionView.registerNib(UINib(nibName: "your_xib_name", bundle: nil), forCellWithReuseIdentifier: "your_reusable_identifier")
-
在cellForItemAtIndexPath中,
let cell : MyCustomView = collectionView.dequeueReusableCellWithReuseIdentifier("your_reusable_identifier", forIndexPath: indexPath) as! MyCustomView
cell.lblName.text = "bla bla" //access your Cell's IBOutlets return cell
- 最后,为了控制单元格的大小,可以覆盖collectionView的代理,或者直接进入你的collectionView,selectcollectionCell并拖动它以匹配你的尺寸:)这就是:)
快乐的编码。 search教程以获得更好的理解。 不能解释所有的代表,因为我最终会在这里写博客。
快乐的编码