【发布时间】:2016-05-20 17:07:06
【问题描述】:
所以我在我的普通 ViewController 中有一个 CollectionView,如果我选择一个单元格并在我的 textField 中输入一些内容并按下保存按钮,它应该会更新 nameLabel 但我不知道该怎么做。有人对此有解决方案吗?
这是我当前的代码:
private let reuseIdentifier: String = "Item"
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
ItemCollection.delegate = self
}
func textEnter() {
var text = textField.text
let indexPath = NSIndexPath()
let cell = ItemCollection.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ItemCell
cell.nameLabel.text = text
}
@IBAction func save(sender: AnyObject, cell: UICollectionViewCell) {
textEnter()
}
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var ItemCollection: UICollectionView!
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ItemCell
cell.backgroundColor = UIColor(red: 0/256, green: 128/256, blue: 255/256, alpha: 0.66)
cell.nameLabel.text = "Test1"
return cell
}
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
}
我的手机:
class ItemCell: UICollectionViewCell {
@IBOutlet weak var nameLbl: UILabel!
}
【问题讨论】:
-
按钮和文本字段的位置...
-
两者都在 ViewController 中
-
当然...我问他们不在牢房里??对
-
最后一个问题:先在textfield中输入文本,然后选择单元格,就可以了……..
-
不,遗憾的是没有。通常,只要点击单元格,就会打开一个 containerView,我在其中配置单元格。我只是把它删掉了,所以它的代码更少,更容易阅读和理解。
标签: ios swift uicollectionview uicollectionviewcell