[英]How to set the collection view cell size exactly equal to the collection view in iOS?
[英]how to set collection view cell as equal row cell for all screen
我正在使用swift 2.o集合视图。 这样一来,一切正常。 但是,当我在不同的屏幕上运行时,每个单元格之间的宽度差距是不同的。 我在集合视图的每一行中需要三个单元格,集合视图中的所有单元格的左,右,上,下分别有8像素的间距。 而且我还需要将单元格设置为拐角半径。 我需要下面的图片:[![在此处输入图片描述] [1]] [1]
但是当我在5s屏幕上运行时,我会像这样,而对于6屏幕就像第二张图片一样:
[![在此处输入图片描述] [2]] [2]
iPhone 6 :
[![在此处输入图片描述] [3]] [3]
看到左,右,底部,顶部之间的间隙不像我的第一幅图像那样均匀地缩小。 我需要像我的第一张照片一样。
我在情节提要中将单元格的最小行空间设置为6。 但我无法像我的初恋一样。
请帮帮我。
我的popvc.swift:
import UIKit
class popVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
var tableData: [String] = ["RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI", "RESTAURANTS", "BANKS","COFFEE","PIZZA", "HOTELS", "TAXI","RESTAURANTS", "BANKS", "COFFEE","PIZZA", "HOTELS", "TAXI"]
var tableImages: [String] = ["img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png", "img7.png", "img8.png", "img9.png", "img10.png", "img11.png", "img11.png", "img1.png", "img2.png", "img3.png", "img4.png", "img5.png", "img6.png"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tableData.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: colvwCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! colvwCell
cell.lblCell.text = tableData[indexPath.row]
cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Cell \(indexPath.row) selected")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在故事板中,将大小检查器的“集合视图”部分的“插入”设置为“顶部= 8”,“底部= 8”,“左= 8”和“右= 8”,其外观应类似于
并返回单元格的大小为
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//If you want your cell should be square in size the return the equal height and width, and make sure you deduct the Section Inset from it.
return CGSizeMake((self.view.frame.size.width/3) - 16, (self.view.frame.size.width/3) - 45);
}
就是这样,仅此而已。 您应该将结果视为我的:
使用以下代码创建集合视图:
@IBOutlet var collectionView: UICollectionView?
var screenSize: CGRect!
var screenWidth: CGFloat!
var screenHeight: CGFloat!
override func viewDidLoad() {
print("select block is \(self.strBlockname)")
screenSize = UIScreen.mainScreen().bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: (screenWidth/3)-1, height: (screenWidth/3)-1)
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1
collectionView = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight-65), collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
collectionView!.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView!)
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 20
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
cell.backgroundColor = UIColor.blackColor()
cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)"
cell.imageView?.image = UIImage(named: "circle")
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
其针对iPhone5s,6s和6+的输出如下:
iPhone 5S
iPhone6s
@ user5513630在集合视图中添加以下代码。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = UIScreen.mainScreen().bounds.size
// 8 - space between 3 collection cells
// 4 - 4 times gap will appear between cell.
return CGSize(width: (size.width - 4 * 8)/3, height: 40)
}
阅读本教程。 它将满足您的要求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.