我使用xib文件为UICollectionView
包装器和可重复使用的UICollectionViewCell
创建了一个Carousel小部件。
窗口小部件由两个UICollectionView
布局组成,一个在另一个上面。顶部UICollectionView
用于捕获用户事件,并控制两个UICollectionView
的内容偏移量容器。我采用这种方法,以分页大小不同于UICollectionView
宽度滚动(按项目大小滚动而不是页面)。
当我用框架初始化时,一切正常,但如果我正在使用故事板,分配UIView我的轮播小部件类,布局就搞乱了
滚动底部轮播后,视图布局正确。
这是我的小部件和单元格的代码:
import UIKit
class CarouselWidgetView: UIView ,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
@IBOutlet weak var dummyCollectionView: UICollectionView!
@IBOutlet weak var mainCollectionView: UICollectionView!
@IBOutlet var mainView: UIView!
@IBOutlet weak var dummyWidth: NSLayoutConstraint!
@IBOutlet weak var topLabel: UILabel!
enum Events : String {
case ON_ITEM_SELECTED = "onItemSelected"
}
var items: NSArray? = nil {
didSet {
//reload data
dummyCollectionView.reloadData()
mainCollectionView.reloadData()
}
}
var frameRatio:CGFloat = 0.9
var didLayoutSubviews = false
var didSetDummyCollection = false
@IBInspectable var framePercent: CGFloat {
get {
return frameRatio
}
set {
frameRatio = newValue
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
NSBundle.mainBundle().loadNibNamed("carouselWidget", owner: self, options: nil)
mainCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
dummyCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
mainView.frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
self.addSubview(mainView)
didLayoutSubviews = true
}
override func layoutSubviews() {
if(!didLayoutSubviews){
NSBundle.mainBundle().loadNibNamed("carouselWidget", owner: self, options: nil)
mainCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
dummyCollectionView.registerNib(UINib(nibName: "carouselCell", bundle: nil), forCellWithReuseIdentifier: "carouselCell")
mainView.frame = CGRectMake(0, 0, self.frame.width, self.frame.height)
self.addSubview(mainView)
didLayoutSubviews = true
}
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if(!didSetDummyCollection){
dummyWidth.constant = mainCollectionView.frame.width * frameRatio + 8
didSetDummyCollection = true;
}
return CGSizeMake(mainCollectionView.frame.width * frameRatio + 4, mainCollectionView.frame.height)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let length = items?.count{
return length
}
return 0
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("carouselCell", forIndexPath: indexPath) as! CarouselCell
cell.cellData = (items![indexPath.item] as! NSDictionary)
cell.frame.origin.y = 0
self.layoutIfNeeded()
return cell
}
func scrollViewDidScroll(scrollView: UIScrollView) {
mainCollectionView.contentOffset = scrollView.contentOffset
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
NSNotificationCenter.defaultCenter().postNotificationName(CarouselWidgetView.Events.ON_ITEM_SELECTED.rawValue, object: ["data":(items![indexPath.item] as! NSDictionary) , "cell":collectionView.cellForItemAtIndexPath(indexPath)!])
}
}
单元格代码:
import UIKit
class CarouselCell: UICollectionViewCell {
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var tags: UILabel!
var cellData:NSDictionary? = nil{
didSet {
if let imgUrl = cellData!["image"] as? String{
backgroundImage.loadImageFromURL(imgUrl)
}
if let poiName = cellData!["name"] as? String{
name.text = poiName
}
if let tagsDesc = cellData!["tags"] as? String{
tags.text = tagsDesc
}
}
}
override init(frame: CGRect) {
super.init(frame: CGRectMake(frame.origin.x, 0, frame.width, frame.height))
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我错过了什么?