我有一个表格 View ,其中包含两种类型的自定义单元格。一旦我点击带有自定义标题的单元格,最后一个单元格就会向下切换,并显示三个文本字段和一个保存按钮。我想要做的是单击“保存”按钮后保存文本字段的值。
这是我构建表格的方式:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if editionMood == .notEditing {
let cell : WeightGoalPlanCell = table.dequeueReusableCell(withIdentifier: "WeightTargetDateCell" ) as! WeightGoalPlanCell
cell.labelOfNumber?.text = String(planArray[indexPath.row].numberOfDays)
cell.labelOfDays?.text = "Days"
cell.labelOfLevel?.text = planArray[indexPath.row].hardshipLevel
cell.labelOfCalorie?.text = "Calories Defeshits"
cell.labelOfWeightLossPerWeek?.text = "Lost per week"
cell.valueOfCalorieDeficit?.text = String(planArray[indexPath.row].calorieDificitValue)
cell.valueOfWeightLossPerWeek?.text = String(planArray[indexPath.row].weightToBeLost)
cell.finalDate?.text = String(planArray[indexPath.row].goalDate)
return cell
}else {
while indexPath.row < ( planArray.count ) {
let cell : WeightGoalPlanCell = table.dequeueReusableCell(withIdentifier: "WeightTargetDateCell" ) as! WeightGoalPlanCell
cell.labelOfNumber?.text = String(planArray[indexPath.row].numberOfDays)
cell.labelOfDays?.text = "Days"
cell.labelOfLevel?.text = planArray[indexPath.row].hardshipLevel
cell.labelOfCalorie?.text = "Calories Defeshits"
cell.labelOfWeightLossPerWeek?.text = "Lost per week"
cell.valueOfCalorieDeficit?.text = String(planArray[indexPath.row].calorieDificitValue)
cell.valueOfWeightLossPerWeek?.text = String(planArray[indexPath.row].weightToBeLost)
cell.finalDate?.text = String(planArray[indexPath.row].goalDate)
return cell
}
let cell : editingCellWeightPlan = table.dequeueReusableCell(withIdentifier: "EditWeightTargetDateCell") as! editingCellWeightPlan
cell.calc?.placeholder = "8"
cell.daysValue?.placeholder = "7"
cell.weeklyLost?.placeholder = "100"
return cell
}
}
这是定制的 Cell 类:
class WeightGoalPlanCell : UITableViewCell {
@IBOutlet weak var labelOfNumber: UILabel!
@IBOutlet weak var labelOfDays: UILabel!
@IBOutlet weak var labelOfLevel: UILabel!
@IBOutlet weak var labelOfCalorie: UILabel!
@IBOutlet weak var labelOfWeightLossPerWeek: UILabel!
@IBOutlet weak var valueOfCalorieDeficit: UILabel!
@IBOutlet weak var valueOfWeightLossPerWeek: UILabel!
@IBOutlet weak var finalDate: UILabel!
}
class editingCellWeightPlan : UITableViewCell {
@IBOutlet weak var daysValue: UITextField!
@IBOutlet weak var calc: UITextField!
@IBOutlet weak var weeklyLost: UITextField!
}
我的问题是文本字段没有全局定义,但为保存按钮提供服务的函数是全局的。我如何获取这些文本字段的值?
最佳答案
根据我对所提供描述的理解,我建议您将单元格替换为带有保存按钮的 tableFooter View 的三个文本字段。然后,您可以轻松地将这些文本字段中的值捕获到 View Controller 中。
关于ios - 如何为自定义单元格内的按钮设置监听器以快速获取文本字段数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643446/