ios - 如何从不同的单元格中获取值?
问题描述
我有一个包含多个原型单元格的表格视图。我想在这些单元格中获取 textField 的值并在点击按钮时附加一个数组。
Cells 有一个叫做 element 的对象。我想将此元素附加到我的视图控制器中的 elementsArray 中。问题是当 cellForRow 方法工作时,这个元素键被创建,但由于 cellForRowAt 方法只工作一次,元素对象的“值”键取 txtContent 的初始值。
为每个单元格编写文本后如何获取 txtContent 文本?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let object = surveyDetailArray.first!.elements[indexPath.row]
switch object.type {
case CellConfig.email.rawValue:
let cell = tableView.dequeueReusableCell(withIdentifier: "EmailCell", for: indexPath) as! EmailCell
cell.lblTitle.text = object.title
cell.txtContent.inputAccessoryView = toolBar
cell.element["formElementId"] = object.id as AnyObject
cell.element["options"] = optionArray as AnyObject
elementsArray.append(cell.element as AnyObject)
return cell
case CellConfig.number.rawValue:
let cell = tableView.dequeueReusableCell(withIdentifier: "NumberCell", for: indexPath) as! NumberCell
cell.lblTitle.text = object.title
cell.txtContent.inputAccessoryView = toolBar
cell.txtContent.keyboardType = .numberPad
cell.element["formElementId"] = object.id as AnyObject
cell.element["options"] = optionArray as AnyObject
elementsArray.append(cell.element as AnyObject)
return cell
}
我的单元班
class EmailCell: UITableViewCell,UITextFieldDelegate {
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var txtContent: CustomTextField!
var element = ["formElementId":"",
"value":"",
"options":[]] as [String : Any]
override func awakeFromNib() {
super.awakeFromNib()
txtContent.backgroundColor = #colorLiteral(red: 0.1570051014, green: 0.1588717997, blue: 0.2081049681, alpha: 0.1)
txtContent.layer.borderWidth = 0
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
txtContent.delegate = self
// Configure the view for the selected state
}
func textFieldDidEndEditing(_ textField: UITextField) {
element["value"] = textField.text!
}
}
解决方案
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let object = surveyDetailArray.first!.elements[indexPath.row]
switch object.type {
case CellConfig.email.rawValue:
let cell = tableView.dequeueReusableCell(withIdentifier: "EmailCell", for: indexPath) as! EmailCell
cell.txtContent.delegate = self
return cell
case CellConfig.number.rawValue:
let cell = tableView.dequeueReusableCell(withIdentifier: "NumberCell", for: indexPath) as! NumberCell
return cell
}
extension ViewController: UITextFieldDelegate{
func textFieldDidEndEditing(_ textField: UITextField) {
var cell: UITableView Cell!
if let emailCell: UITableViewCell = textField.superview.superview as? EmailCell{
cell = emailCell
}
if let numbCell: UITableViewCell = textField.superview.superview as? NumberCell{
cell = numbCell
}
var table: UITableView = cell.superview as UITableView
let textFieldIndexPath = table.indexPathForCell(cell)
// HERE DO THE UPDATION YOU WANT TO DO
}
}
推荐阅读
- python - Django ignore_conflicts 不适用于用户模型
- angular - Angular 8 Jasmine 不匹配 HTTP 请求
- api-platform.com - API 平台 - 我应该使用哪种方法来创建没有实体的自定义操作
- apache-spark - 如何以编程方式为 spark 中的所有字段生成 Struct Type 作为 StringType?
- android - 为什么 notifyDataSetChanged 会炸毁 ViewPager2 中的 PagerTransformer?
- sql - SQL 在发货和退货日期之间按订单 ID 和产品代码创建表 wkly 收入
- gradle - 使用 Gradle 5.X 在 Azure DevOps 上生成代码覆盖率报告
- java - 反句去重
- windows - 如果正在编辑绘图草图,如何获取?(Autodesk Inventor C++ API)
- swiftui - 在 SwiftUI 的上下文中,Single Source of Truth (SSOT) 的含义是什么?