我想用UICollectionView創建一個網格,用戶可以通過y單元格(在文本框中輸入)將它設置爲x單元格,同時仍然佔用屏幕上的相同寬度。swift中的UICellView單元格佈局
我已經能夠創建一個網格視圖,其中包含正確的單元格數量,但它們還沒有在正確的佈局中,即輸入5乘以7會得到35個單元,但不是在5單元乘7單元佈局。這是迄今爲止的代碼。
MyCollectionViewCell.swift 進口的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
}
}
我已經制約了UICollectionView在左,右,上,並希望以編程方式調整細胞的集合視圖爲UICollectionView.width/x(用戶選擇的單元格寬度)。
不確定最好的方法來做到這一點,也需要保持單個單元格之間的一些空白區域。任何幫助將非常感激!