使用 UICollectionViewCell 内的 Textfield 更新标签

Posted

技术标签:

【中文标题】使用 UICollectionViewCell 内的 Textfield 更新标签【英文标题】:update Label with Textfield inside UICollectionViewCell 【发布时间】: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,我在其中配置单元格。我只是把它删掉了,所以它的代码更少,更容易阅读和理解。 【参考方案1】:

要完成这项工作,您需要做几件事。

首先,您需要通过存储“引用”来保留单击单元格的上下文。最简单的方法是引入三个新变量——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
    


【讨论】:

我明白你的意思,但我对 swift 还是有点陌生​​,这在代码中到底是什么样的?谢谢! 刚刚更新了您的代码的修改版本。 非常感谢!但是didSelect方法有一个错误:Objective-C方法'collectionView:didSelectItemAtIndexPath:'方法提供的'collectionView(:didSelectItemAtIndexPath:)'与可选要求方法'collectionView(:didSelectItemAtIndexPath:)冲突' 在协议 'UICollectionViewDelegate' 中 -> Bool 是从复制粘贴中遗留下来的。尝试删除它。 该死的总是小事。但无论如何,非常感谢您的帮助!

以上是关于使用 UICollectionViewCell 内的 Textfield 更新标签的主要内容,如果未能解决你的问题,请参考以下文章