[英]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.