【问题标题】:Pass data from UICollectionViewCell to another UICollectionViewCell in code without storyboards using Swift使用 Swift 将数据从 UICollectionViewCell 传递到代码中的另一个 UICollectionViewCell 而无需情节提要
【发布时间】:2018-06-29 07:02:02
【问题描述】:

我有一个UICollectionViewCell(比如UICollectionViewCell 1),它有一个按钮,单元格内有UITextfields。连接到 (ViewController 1) 的 UICollectionViewCellViewController 1 推送到 detailedCollectionViewCell,即 (ViewController 2),该 ViewController 2 在单元格内还包含一个按钮和 UITextfields

这是通过协议和委托完成的。我的问题,当点击第一个UICollectionViewCell (@987654336) 中的按钮时,我想将位于UICollectionViewCell (ViewController 1) 中的UITextfields 中的数据传递给detailedViewCell UITextfield (ViewController 2) @)。

这是我的代码:

//Protocol View Controller 1

protocol InfoCellDelegate: class {
   func buttonToDetailCell()
}

// Inside ViewController 1 class

func buttonToDetailCell() {
    let vc = InfoViewController(collectionViewLayout: UICollectionViewFlowLayout())
    navigationController?.pushViewController(vc, animated: true)
}

// UICollectionViewCell 

weak var delegate: InfoCellDelegate?

@objc func InfoButtonPressed(_ sender: UIButton) {
    if cellOne.text != "" {
         delegate?.buttonToDetailCell()
    }
 }

【问题讨论】:

    标签: swift uicollectionviewcell viewcontroller pass-data


    【解决方案1】:

    由于您使用的是自定义委托协议,因此您可以控制可以将哪些参数传递给函数。

    protocol InfoCellDelegate: class {
        func buttonToDetailCell(text: String?)
    }
    
    delegate?.buttonToDetailCell(text: textField.text)
    

    话虽如此,我认为更简洁的解决方案是不使用按钮,直接使用collectionView(_:, didSelectItemAt:) 捕获集合视图选择事件。 然后您可以从单元格中检索值。

    // in VC1
    collectionView.delegate = self
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? MyCell
        let text = cell?.textField.text
        // do something with text
    }
    

    编辑: 这是一些半完整的伪代码,可以完成您所追求的事情。

    protocol ButtonDelegate: class {
        func buttonTapped(text: String?)
    }
    
    class ViewControllerOne: UIViewController, UICollectionViewDataSource, ButtonDelegate {
        func buttonTapped(text: String?) {
            let vc = ViewControllerTwo(nibName: nil, bundle: nil)
            vc.text = text
            self.present(vc, animated: true, completion: nil)
        }
    
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell: MyCell = collectionView.dequeueReusableCell... // etc.
            cell.delegate = self
            return cell
        }
    }
    
    class ViewControllerTwo: UIViewController {
        var text: String?
    }
    
    class MyCell: UICollectionViewCell {
        var button = UIButton()
        var textField = UITextField()
        weak var delegate: ButtonDelegate?
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        }
    
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        @objc private func buttonTapped(_ sender: UIButton) {
            self.delegate?.buttonTapped(text: self.textField.text)
        }
    }
    

    【讨论】:

    • 感谢@Samah 回复我,非常感谢!我正在尝试添加您建议的代码,但它不起作用。我可能做错了什么。我无法将数据传递给第二个控制器。
    • 添加了代码 sn-p,如果您愿意,可以复制/粘贴并填写空白。我仍然建议使用collectionView(_:, didSelectItemAt:) 而不是按钮,但我猜这取决于你的设计师。 :)
    猜你喜欢
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    相关资源
    最近更新 更多