Layout da célula UICellView em rápida
Estou tentando usar um UICollectionView para criar uma grade que um usuário pode definir como x células por y células (inseridas nas caixas de texto), enquanto ainda ocupa a mesma largura na tela.
Consegui criar uma exibição em grade que contém o número correto de células, mas ainda não estão no layout correto, ou seja, digitar 5 por 7 dará 35 células, mas não em um layout de 5 células por 7 células. Este é o código que tenho até agora.
MyCollectionViewCell.swift import UIKit
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
}
}
Restringi o UICollectionView à esquerda, direita e superior e esperava redimensionar programaticamente as células na exibição de coleção para serem UICollectionView.width / x (o número de células largas escolhidas pelo usuário).
Não tem certeza da melhor maneira de fazer isso e também precisa manter algum espaço em branco entre as células individuais. Qualquer ajuda seria muito apreciada!