gpt4 book ai didi

ios - 如何将 Collection View 单元格设置为所有屏幕的相等行单元格

转载 作者:搜寻专家 更新时间:2023-11-01 05:47:36 24 4
gpt4 key购买 nike

我正在使用 swift 2.o Collection View 。在这一切工作正常。但是当我在不同的屏幕上运行时,每个单元格之间的宽度差距是不同的。我在 Collection View 的每一行中需要三个单元格, Collection View 中所有单元格的左、右、上、下有 8 px 的间隙。而且我还需要将单元格设置为角半径。我需要如下图:[![在此处输入图片描述][1]][1]

但是当我在 5s 屏幕上运行时,我会像这样,对于 6 屏幕,如第二张图片:

[![在此处输入图片描述][2]][2]

苹果 6:

[![在此处输入图片描述][3]][3]

请看左、右、下、上之间的间隙并不像我的第一张图片那样均匀。我需要像我的第一张图片一样。

我在 Storyboard中将单元格的最小行间距设置为 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.
}


}

最佳答案

从尺寸检查器中的 Storyboard设置 CollectionViews Section Inset 为 Top = 8,Bottom = 8,Left = 8 和 Right = 8,它应该看起来像

enter image description here

并返回单元格的大小为

- (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);
}

仅此而已,仅此而已。你应该看到我的结果:

enter image description here

enter image description here

enter image description here

关于ios - 如何将 Collection View 单元格设置为所有屏幕的相等行单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35864049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com