【问题标题】:How to store the text based on the tag如何根据标签存储文本
【发布时间】:2018-09-24 05:29:21
【问题描述】:

如何根据标签存储文本。

我在UITableViewCell 中使用了UITextField

我的视图控制器:-

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let identifier = "Cell"
        var cell: NH_QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell

        if cell == nil {
            tableView.register(UINib(nibName: "NH_QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell
        }
        cell.contentView.backgroundColor = UIColor.clear

        cell.textadd = {[weak self] in

                if let i = self?.tableview.indexPath(for: $0) {

                    print("the selected text\(cell.textname ?? "")")

                    print(i)

                    print("the Selected button is \(cell.textname ?? "")")

                    print(i)

                    cell.setText(Options:(self?.questionViewModel.datafordisplay(atindex: indexPath))!)                     

                    let model = self?.questionViewModel.datafordisplay(atindex: indexPath)
                    print(model?.values ?? "")

                    if model?.isSelected == true{

                     cell.texttype.tag = indexPath.row
                   //  cell.texttype.text = questionViewModel.datafordisplay(atindex: indexPath)

      }
      return cell

}

tableviewcell:-

 @IBOutlet weak var texttype: UITextField!
    var textname:String?

    var textadd : ((NH_QuestionListCell) -> Void)?
    @IBAction func TextAction(_ sender: UITextField) {

         print(texttype.text)
            //    self.question.text = texttype.text

        let index = texttype.tag
        self.textname = texttype.text          

        self.textname = texttype.text
        print(self.textname)
                print(texttype.text)
             textadd?(self)
    }

    func setText(Options:NH_OptionsModel)     
    {
        self.displaySmiley.isHidden = true
        self.viewdisplay.isHidden = false
        self.imagebutton.isHidden = true
        self.question.isHidden = true
        self.displayRatingView.isHidden = true

        print(Options.values)
        Options.values = self.textname
        print(Options.values)     
    }

     func textFieldDidBeginEditing(_ textField: UITextField) {
       texttype.text = ""    
    }

    func textFieldShouldClear(_ textField: UITextField) -> Bool
    {
               return true
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
          self.TextAction(texttype)          
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {         
        return true;
    }      

    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {           
        return true;       
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        print(texttype.text)

        texttype.resignFirstResponder()

        return true
    }

我得到了输出。在第一个单元格中,我在文本字段中输入了一些文本,然后点击“输入”。然后我滚动。当时我发现我输入的文本在其他单元格中可用。如何解决这个问题?

【问题讨论】:

  • 如何解决问题?
  • 不清楚你在问什么,代码很杂乱无章。
  • @Rigo 我以这种方式编码,所以现在我的问题是首先我在文本字段中输入一个文本,然后在下一个单元格中我需要输入文本但这里显示第一个文本第二个单元格中的单元格。因此,在滚动时,每个单元格中都会显示相同的文本。那么如何修复它?
  • @Rigo 如何解决问题?

标签: ios uitableview


【解决方案1】:

您不需要使用单元格tag,因为您使用每个单元格的闭包来处理文本字段更新;此闭包将捕获indexPath,因此您知道要更新哪一行。唯一可能出现的问题是,如果您允许单元格重新排序,无论是通过单元格移动还是插入/删除单元格的能力。在这种情况下,捕获的indexPath 将变为无效(这与使用tag 时遇到的问题相同)。您可以通过在闭包中调用indexPath(for:) 来解决此问题,以在闭包执行时获取单元格的索引路径。

滚动时出现文本的原因是因为单元格对象被重复使用并且您没有明确设置文本字段文本。

最后,您可以避免整个weak self 跳舞,因为当视图控制器被释放时,表格视图将被释放,这意味着单元格将被释放。尽管使用闭包,但您没有保留周期。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let identifier = "Cell"
        var cell: NH_QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell

        if cell == nil {
            tableView.register(UINib(nibName: "NH_QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell
        }
        cell.contentView.backgroundColor = UIColor.clear
        cell.setText(options: self.questionViewModel.datafordisplay(atindex: indexPath))

        cell.textadd = {

                self.questionViewModel.setData(atIndex: indexPath,text: cell.textName)
        }
      return cell

}

请注意,您尚未提供有关如何更新模型对象的详细信息,因此我插入了一个函数调用来展示这个想法。

我还建议您阅读 Swift 约定。使用大写字母作为参数名称和使用NH_ 作为类前缀不是“Swifty”

【讨论】:

  • setData中应该写什么?
  • 大概你想用用户输入的文本更新你的模型?
  • 我已经更新了 didselect 方法。在视图模型中应该做什么
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多