我是iOS和Swift的新手。
我无法获取我在UICollectionView单元格中存在的多个UITextFiled中手动输入的数据
我想在用户开始编辑文本后立即从每个文本字段中连续获取数据,然后将其压入对象的变量中。
单元格样本-将有多个单元格
例如,以上链接中提供的图像是一个单元格的示例,该单元格包含多个文本字段,标签和按钮
现在我想从每个单元格中获取所有数据并将其存储在对象数组中
我的ViewController
extension SampleViewController:UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SampleCollectionViewCell;
return cell;
}
}
我的模型对象类
class Singleton{
static let shared = Singleton()
var list = [CellFields].self
}
class CellFields {
var button1:bool
var button2:bool
var dropdown:String
var field1:String
var field2:String
var field3:String
var label1:String
var label2:String
}
我的UICollectionViewCell
class MySampleCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var dropdown: DropDown!
@IBOutlet weak var field1: UITextField!
@IBOutlet weak var field2: UITextField!
@IBOutlet weak var field3: UITextField!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
}
我尝试了所有委托方法,但仍然无法实现我想要的。
我想要获得的结果与此类似
Singleton.shared.list
[0]
dropdown = "Dropdown"
field1 = "Something"
field2 = "Random"
field3 = "Another"
label1 = "Label"
label2 = "Label2"
button1 = true
button2 = false
[1]
dropdown = "Dropdown1"
field1 = "Something1"
field2 = "Random2"
field3 = "Another3"
label1 = "Label4"
label2 = "Label3"
button1 = false
button2 = true
...
...
...
答案 0 :(得分:0)
您不需要单例即可创建类对象数组:
let classFieldSets: [ClassFields] = []
在模型类中创建一个初始化方法:
通过SampleViewController扩展程序中的cellForItemAt
将它们传递到单元格中。
答案 1 :(得分:0)
创建类似于以下内容的内容:
// create your own delegate type for the cell
protocol MyCellDelegate {
func myTextFieldChanged(_ tf: UITextField) // call when the textfield changes
func myOtherTextFieldChanged(_ tf: UITextField) // call when the other textfield changes
func myToggleChanged(_ sw: UISwitch) // call when the switch changes
}
class MyCell: UICollectionViewCell {
@IBOutlet private var myTextField: UITextField!
@IBOutlet private var myOtherTextField: UITextField!
@IBOutlet private var myToggle: UISwitch!
private var _delegate: MyCellDelegate? // instance of above protocol type, this will generally be your VC
func initialize(withDelegate delegate: MyCellDelegate) {
myTextField.delegate = self
myOtherTextField.delegate = self
self._delegate = delegate // a textfield uses a delegate pattern
self.myToggle.addTarget(self, action: #selector(toggleValueChanged(_:)), for: .valueChanged) // a switch uses this target/selector pattern
}
}
// I like putting delegate implementations in extensions
extension MyCell: UITextFieldDelegate {
// called when a textfield changes
func textFieldDidEndEditing(_ textField: UITextField) {
if textField == myTextField {
_delegate?.myTextFieldChanged(textField) // call the protocol's method to signal the VC of the change
}
if textField == myOtherTextField {
_delegate?.myOtherTextFieldChanged(textField) // call the protocol's method to signal the VC of the change
}
}
}
extension MyCell {
// @objc is required for the target/selector pattern
@objc func toggleValueChanged(_ toggle: UISwitch) {
if toggle == myToggle {
_delegate?.myToggleChanged(toggle)
}
}
}
然后在您的VC的cellForItemAt中:
let cell=collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SampleCollectionViewCell;
cell.initialize(withDelegate: self)
return cell;
在您的VC中,也将其设置为MyCellDelegate
,并带有:
extension SampleViewController: MyCellDelegate{
func myTextFieldChanged(_ tf: UITextField) {
// save new textfield value
}
func myOtherTextFieldChanged(_ tf: UITextField) {
// save new other textfield value
}
func myToggleChanged(_ sw: UISwitch) {
// save new toggle value
}
}
理想情况下,您将创建一个单一的方法来立即更新整个表单,但这实际上取决于您拥有的数据类型以及可选的数据,我将把它作为一个挑战。但是,至少从这开始,您应该能够使您的表单正常工作,并了解所有委托内容的状况。