Ios UICellView在swift中的单元格布局
我试图使用UICollectionView创建一个网格,用户可以将其设置为x单元格乘以y单元格(在文本框中输入),同时仍然占据屏幕上相同的宽度 我已经能够创建一个网格视图,其中包含正确数量的单元格,但这些单元格的布局还不正确,即输入5乘7将得到35个单元格,但不是5乘7的单元格布局。这是我目前掌握的代码 MyCollectionViewCell.swift 导入UIKitIos UICellView在swift中的单元格布局,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我试图使用UICollectionView创建一个网格,用户可以将其设置为x单元格乘以y单元格(在文本框中输入),同时仍然占据屏幕上相同的宽度 我已经能够创建一个网格视图,其中包含正确数量的单元格,但这些单元格的布局还不正确,即输入5乘7将得到35个单元格,但不是5乘7的单元格布局。这是我目前掌握的代码 MyCollectionViewCell.swift 导入UIKit class MyCollectionViewCell: UICollectionViewCell { @IBOut
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet var cellLabel: UILabel!
}
CreateNewProject.swift
class CreateNewProject : UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITextFieldDelegate {
@IBOutlet var widthTextBox: UITextField!
@IBOutlet var lengthTextBox: UITextField!
@IBOutlet var myCollection: UICollectionView!
let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
@IBAction func updateView(sender: AnyObject) {
let widthValue = Int(widthTextBox.text!)
let lengthValue = Int(lengthTextBox.text!)
multiplyValue = Int(widthValue! * lengthValue!)
createArray()
}
func createArray() {
var i = 0
while i < multiplyValue {
items.append("")
i += 1
}
myCollection.reloadData()
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(self.items.count)
}
// make a cell for each cell index path
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
// get a reference to storyboard cell
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
// Use the outlet in custom class to get a reference to the UILabel in the cell
cell.cellLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor.lightGrayColor()
return cell
}
}
class CreateNewProject:UIViewController、UICollectionViewDataSource、UICollectionViewDelegate、UIExtFieldDelegate{
@IBEXTEXTBOX:UITextField!
@IBOutlet var lengthTextBox:UITextField!
@IBMCollection:UICollectionView!
让reuseIdentifier=“cell”//也在情节提要中输入此字符串作为单元标识符
@iAction func updateView(发件人:AnyObject){
让widthValue=Int(widthTextBox.text!)
让lengthValue=Int(lengthTextBox.text!)
multiplyValue=Int(宽度值!*长度值!)
createArray()
}
func createArray(){
变量i=0
而我是多重价值的{
项目。附加(“”)
i+=1
}
myCollection.reloadData()
}
func collectionView(collectionView:UICollectionView,numberOfItemsInSection:Int)->Int{
打印(self.items.count)
}
//为每个单元格索引路径创建一个单元格
func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell{
//获取对情节提要单元的引用
让cell=collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier,forIndexPath:indexPath)作为!MyCollectionViewCell
//使用自定义类中的outlet获取对单元格中UILabel的引用
cell.celllable.text=self.items[indexPath.item]
cell.backgroundColor=UIColor.lightGrayColor()
返回单元
}
}
我已将UICollectionView限制在左侧、右侧和顶部,并希望通过编程将集合视图中的单元格大小调整为UICollectionView.width/x(用户选择的单元格宽度数量)
不确定这样做的最佳方法,还需要在单个单元格之间保留一些空白。任何帮助都将不胜感激 以下是要添加到collectionView的代码。属性
cellscaross
、cellsDown
、horizGap
和vertGap
是您可以在应用程序中设置的值。horizGap
和vertGap
在方法minimuminetemspaccingforsectionatindex
和minimumlinespaccingforsectionatindex
中设置SizeFormiteIndeXPath
将计算单元格的必要维度以使其正常工作
var cellsAcross = 5
var cellsDown = 7
var horizGap = 4
var vertGap = 4
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cellsAcross * cellsDown
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return CGFloat(vertGap)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return CGFloat(horizGap)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// Compute the dimensions of a cell for a cellsAcross x cellsDown layout.
let dimH = (collectionView.bounds.width - (CGFloat(cellsAcross) - 1) * CGFloat(horizGap)) / CGFloat(cellsAcross)
let dimV = (collectionView.bounds.height - (CGFloat(cellsDown) - 1) * CGFloat(vertGap)) / CGFloat(cellsDown)
return CGSize(width: dimH, height: dimV)
}
对于此演示,我添加了以下代码:
@IBAction func threeBy5(sender: UIButton) {
cellsAcross = 3
cellsDown = 5
collectionView.reloadData()
}
@IBAction func fourBy6(sender: UIButton) {
cellsAcross = 4
cellsDown = 6
collectionView.reloadData()
}
@IBAction func fiveBy7(sender: UIButton) {
cellsAcross = 5
cellsDown = 7
collectionView.reloadData()
}
// Handle screen rotation
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
collectionView.reloadData()
}
简单地说,添加视图控制器的扩展以实现
UICollectionViewDelegateFlowLayout
协议:
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let size = collectionView.frame.size.width / CGFloat(cols) - CGFloat((cols - 1)) * spacingBetweenCells
return CGSize(width: size, height: size)
}
}
SpacingBetween单元格
在此表示要在单元格之间放置的间距。请尝试以下代码:
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet var collectionView: UICollectionView!
let rows = 7
let columns = 3
let spaceBetweenRows = 2
let spaceBetweenColumns = 4
var cellHeight: CGFloat {
get {
return collectionView.frame.height/CGFloat(rows)-CGFloat(spaceBetweenRows)
}
}
var cellWidth: CGFloat {
get {
return collectionView.frame.width/CGFloat(columns)-CGFloat(spaceBetweenColumns)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
collectionView.dataSource = self
collectionView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return rows*columns
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
return collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return CGFloat(spaceBetweenRows)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return CGFloat(spaceBetweenRows)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: cellWidth, height: cellHeight)
}
}
主故事板
Swift 4.2版本,用于@vacawama提供的答案:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(vertGap)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(horizGap)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Compute the dimensions of a cell for a cellsAcross x cellsDown layout.
let dimH = (collectionView.bounds.width - (CGFloat(cellsAcross) - 1) * CGFloat(horizGap)) / CGFloat(cellsAcross)
let dimV = (collectionView.bounds.height - (CGFloat(cellsDown) - 1) * CGFloat(vertGap)) / CGFloat(cellsDown)
return CGSize(width: dimH, height: dimV)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(vertGap)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(horizGap)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// Compute the dimensions of a cell for a cellsAcross x cellsDown layout.
let dimH = (collectionView.bounds.width - (CGFloat(cellsAcross) - 1) * CGFloat(horizGap)) / CGFloat(cellsAcross)
let dimV = (collectionView.bounds.height - (CGFloat(cellsDown) - 1) * CGFloat(vertGap)) / CGFloat(cellsDown)
return CGSize(width: dimH, height: dimV)
}