删除表格视图单元格中的UIlabel文本
我有一个表格视图并为此使用自定义单元格。现在我在我的酒吧里设置了一个清晰的按钮。现在单击该UIBarButton,我想清除单元格中文本字段内的所有文本。我怎样才能做到这一点..??删除表格视图单元格中的UIlabel文本
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 
    } 
    } 
现在单元格包含一个txtAnswer作为UITextField。我如何清除txtAnswer的文本字段。
清除字段:
func clearView(sender:UIButton) { 
    print("Clear Button clicked") 
} 
回答:
你可以得到的tableView的所有可见单元格。
@IBAction func deleteText(_ sender: Any) {     for cell in tableView.visibleCells { 
     if let questionCell = cell as? QuestionWithTextField { 
     // Hide your label here. 
     // questionCell.lblQuestion.hidden = true 
     } 
    } 
} 
回答:
上述代码仅适用于可见的单元格。如果在手机中不可见,单元格值将不会被清除。
为此,您需要遍历每个表视图单元格。我认为这是你最好的选择之一。
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 
       } 
      } 
     } 
    } 
以上是 删除表格视图单元格中的UIlabel文本 的全部内容, 来源链接: utcz.com/qa/261432.html

