gpt4 book ai didi

ios - 使用 UICollectionViewCell 内的 Textfield 更新标签

转载 作者:行者123 更新时间:2023-11-30 13:24:50 24 4
gpt4 key购买 nike

所以我在普通的 ViewController 中有一个 CollectionView,如果我选择一个单元格并在文本字段中输入一些内容并按保存按钮,它应该更新 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!

}

最佳答案

要完成这项工作,您需要做一些事情。

首先,您需要通过存储对其的“引用”来保留单击单元格的上下文。最简单的事情是引入三个新变量 - 部分和项目。使用这些值,您将能够重新创建索引路径对象并引用单元格。

其次,您应该使用上一步中保存的值构造新的 Index Path 对象并调用 cellForItemAtIndexPath(_indexPath: NSIndexPath),而不是调用 dequeueReusableCellWithReuseIdentifier()。如果单元格不在 View 中,这可能会返回零。

注意:仅当用户点击“保存”时才会设置标签,如果单元格滚动到 View 之外然后再返回,文本很可能会丢失。为了保留这一点,您应该在 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
}

}

关于ios - 使用 UICollectionViewCell 内的 Textfield 更新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37352179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com