【发布时间】:2016-09-17 11:49:59
【问题描述】:
我尝试做一个UICollectionView 在屏幕上打印 1,5 单元格,并且通过滚动您应该能够显示下一个单元格。
但这是问题所在,我设法显示 1,5 单元格,但无法滚动。
我不知道为什么,当我将布局scrollDirection 属性设置为.Horizontal 时,会创建一个垂直滚动。
这里是布局和UICollectionView初始化:
let screenSize = UIScreen.mainScreen().bounds
let widthImage = (screenSize.width - CGFloat(8)) / 1.5
let heightImage = UIUtils.getHeightAspectRatio(widthImage)
//let sizeCollectionView: CGSize = CGSize(width: widthImage, height: heightImage)
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: widthImage, height: heightImage)
howTo = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
howTo!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "howToCell")
howTo!.tag = 0
howTo!.delegate = self
howTo!.dataSource = self
self.view.addSubview(howTo!)
howTo!.snp_makeConstraints { (make) -> Void in
make.top.equalTo(readMoreText.snp_bottom).offset(5)
make.leading.equalTo(self.view).offset(5)
make.trailing.equalTo(self.view)
make.height.equalTo(115)
}
howTo?.backgroundColor = UIColor.whiteColor()
howTo?.contentMode = .ScaleToFill
howTo?.scrollEnabled = true
这是显示单元格时调用的方法:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView.tag == howTo!.tag {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("howToCell", forIndexPath: indexPath)
if let url = NSURL(string:UIUtils.getImageUrl(self.exercise.getHowTos()[indexPath.row].getImages(), label: Image.mediumWide)) {
let screenSize = UIScreen.mainScreen().bounds
let widthImage = (screenSize.width - CGFloat(8)) / 1.5
let heightImage = UIUtils.getHeightAspectRatio(widthImage)
let img = UIImageView(frame: CGRect(x: 0, y: 0, width: widthImage, height: heightImage))
img.af_setImageWithURL(url)
cell.contentView.addSubview(img)
cell.backgroundView = cell.contentView
} else {
let img = UIImageView(frame: cell.frame)
img.image = R.image.appIconTemp()
cell.contentView.addSubview(img)
cell.backgroundView = cell.contentView
//cell.backgroundColor = UIColor.blackColor()
}
如果你知道为什么我不能滚动你会成功的!
谢谢
【问题讨论】:
标签: ios swift uicollectionview horizontal-scrolling