所以我在普通的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!
}
答案 0 :(得分:0)
要完成这项工作,您需要做几件事。
首先,您需要通过存储"引用"来保留所单击单元格的上下文。它。最简单的方法是引入三个新变量 - section和item。使用这些值,您将能够重新创建索引路径对象并引用该单元格。
其次,不应调用dequeueReusableCellWithReuseIdentifier(),而应使用上一步中保存的值构造新的Index Path对象,并调用cellForItemAtIndexPath(_ indexPath:NSIndexPath)。如果单元格不在视图中,则可能返回nil。
注意:这将仅在用户点击“保存”时设置标签,如果单元格滚出视图然后返回,则文本很可能会丢失。为了保留这一点,您应该在cellForItemAtIndexPath方法中创建单元格时将输入的值存储在变量中(或直接引用文本框)。
修改后的代码示例(某些语法可能已关闭,我是从内存中执行此操作):
private let reuseIdentifier: String = "Item"
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UIGestureRecognizerDelegate, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
ItemCollection.delegate = self
}
// Store the clicked item location
private section: Int = 0
private item: Int = 0
func textEnter() {
var text = textField.text
let indexPath = NSIndexPath(forItem: item inSection:section)
let cell = ItemCollection.cellForItemAtIndexPath(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
}
// Need to capture the tapped cell in here
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
section = indexPath.section
item = indexPath.item
}
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
}