2017-03-08 113 views
1

我有一个表格视图并为此使用自定义单元格。现在我在我的酒吧里设置了一个清晰的按钮。现在单击该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") 


} 
+0

按下“清除”按钮后,你想要在'dataSource'中的数据发生什么?你想保留数据,只是清除标签? – Eendje

+0

清除UIText字段 – Sam

+0

问题是要求'txtAnswer',而不是'lblQuestion'。为什么下面的答案是指'lblQuestion'? – chengsam

回答

1

你可以得到的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 
     } 
    } 
} 
+0

删除txtAnswer UITextField .. ?? – Sam

+0

如果你想隐藏,只是使用隐藏的属性'questionCell.lblQuestion.hidden = true' –

+0

谢谢它的工作 – Sam

2

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

为此,您需要遍历每个表视图单元格。我认为这是你最好的选择之一。

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 

       } 
      } 
     } 
    } 
相关问题