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