ios - 如何为自定义单元格内的按钮设置监听器以快速获取文本字段数据?

标签 ios swift uitableview

我有一个表格 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!

}

我的问题是文本字段没有全局定义,但为保存按钮提供服务的函数是全局的。我如何获取这些文本字段的值?

enter image description here

最佳答案

根据我对所提供描述的理解,我建议您将单元格替换为带有保存按钮的 tableFooter View 的三个文本字段。然后,您可以轻松地将这些文本字段中的值捕获到 View Controller 中。

关于ios - 如何为自定义单元格内的按钮设置监听器以快速获取文本字段数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643446/

相关文章:

iphone - 重新加载自己创建的 UITableViewCell

ios - 带有 UITableView Swift 3 的可折叠输入表单

ios - 直观地改进 iOS 11leadingSwipeActions 的异步操作

ios - 新录制视频的 MediaFile.getFormatData() 包含错误信息

swift - 访问 tableView 单元格中的所有 UILabel 对象

ios - 访问 heightForRowAtIndexPath 中的自定义单元格属性

javascript - 获取所选文本相对于主 div 的范围(包含所有文本)

ios - TableView 项目应该加载另一个 TableView

swift - 将 UITableView 与 Combine DataSource 绑定(bind)

swift:无法将 [sic] 字符数组缩减为字符串