ios - 从 TableView 单元格中删除 UIlabel 文本

标签 ios swift uitextfield

我有一个表格 View 并为此使用自定义单元格。现在我在我的栏中设置了一个清除按钮。现在单击该 UIBarButton 我想清除单元格中文本字段内的所有文本。我该怎么做...??

  var DataSource = [NewAssessmentModel]()

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.DataSource.count
}

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  let model = self.DataSource[indexPath.row]


    switch(model.assessmentControlType)
    {
    case .text:
         let cell = (tableView.dequeueReusableCellWithIdentifier("QuestionWithTextField", forIndexPath: indexPath) as? QuestionWithTextField)!
         cell.model = model
         cell.indexPath = indexPath
         cell.txtAnswer.delegate = self
         cell.lblQuestion.text = model.labelText
         cell.indexPath   = indexPath

        return cell
  }
  }

现在单元格包含一个作为 UITextField 的 txtAnswer。如何清除 txtAnswer 的文本字段。

用于清除字段:

func clearView(sender:UIButton)
{
   print("Clear Button clicked")


}

最佳答案

以上代码仅适用于可见的单元格,如果在手机中不可见,则不会清除单元格值。

为此,您需要遍历每个表格 View 单元格。我认为这对您来说是不错的选择之一。

    func clearView(sender:UIButton)
    {
        print("Clear Button clicked")
        for view: UIView in tableView.subviews {
            for subview: Any in view.subviews {
                if (subview is UITableViewCell) {
                    let cell = subview as? UITableViewCell
                    // do something with your cell

                    if let questioncell = cell as? QuestionWithTextField
                    {
                        questioncell.txtField.text = ""
                    }

                    // you can access any cells

                }
            }
        }
    }

关于ios - 从 TableView 单元格中删除 UIlabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666570/

相关文章:

ios - 为什么 @import (module) 在 Cocos2d ios 中不起作用?

ios - PerformSegueWithIdentifier 和 rootViewController 的问题

javascript - 在 Facebook/Messenger 中打开深层链接时无法打开 native 应用程序(在其他应用程序中工作...)

ios - swift:在不同的 UIImageView 中使用相同的图像?

ios - UITextField 文本未保存

ios - xcode 将不同的最大字符长度设置为不同的 TextField

ios - 将 NSSet 转换为 NSMutableArray

ios - 代码 : Why same storyboard design display differently on simulator and device?

objective-c - SwiftUI 协调器 : method can not be marked @objc

c# - Monotouch UITextField 只有数字