2017-03-08 4 views
1

iテーブルビューがあり、これにカスタムセルを使用します。今私はバーに明確なボタンを設定しています。その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 
    } 
    } 

ここで、セルには、UITextFieldとしてtxtAnswerが含まれています。どのように私はtxtAnswerのテキストフィールドをクリアすることができます。フィールドをクリアするための

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


} 
+0

「クリア」ボタンを押した後、「データソース」のデータはどうしますか?データを保持し、ラベルを消去したいですか? – Eendje

+0

UITextフィールドをクリアしてください – Sam

+0

質問は 'lblQuestion'ではなく' txtAnswer'を求めています。なぜ以下の答えが '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

上記のコードは、表示されているセルのみに適用されます。セルの値が電話機に表示されていない場合、クリアされません。

このためには、すべてのテーブルビューセルをループする必要があります。私はこれがあなたのための良い選択の一つだと思います。

関連する問題