首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在tableview.reload ()之后,TextField数据消失

在tableview.reload ()之后,TextField数据消失
EN

Stack Overflow用户
提问于 2018-10-25 00:11:30
回答 1查看 415关注 0票数 -1

我有一个表格视图,它在单元格中有一个文本字段(以编程方式添加)。我使用editActionsForRowAt方法来插入和删除单元格,但是每次我插入/删除时,我之前输入的文本字段数据都会消失。我想是因为tableview.reload方法。你能给我一些建议吗?

非常感谢!

代码语言:javascript
复制
 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let myAction = UITableViewRowAction(style: .normal, title: "INSERT") { (action, indexPath) in

        print("I'm here")

      self.data[indexPath.section].subType.insert("Set", at: indexPath.item)
     //   self.data[indexPath.section].subType.insert("Set", at: indexPath.section)
        print("inserttt")
        self.tblView.reloadData();

    }

    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
       // if editingStyle == .delete{
            print("delete")

            self.data[indexPath.section].subType.remove(at: indexPath.row)
            self.tblView.reloadData()

       // }
    }

  //  tableView.keyboardDismissMode = .onDrag
    return [myAction,delete]
}






    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cells")
  //  cell?.textLabel?.text = data[indexPath.section].subType[indexPath.row]
    cell?.textLabel?.text = data[indexPath.section].headerName



    var sampleTextField =  UITextField(frame: CGRect(x: 180, y: 10, width: 60, height: 40))
    sampleTextField.borderStyle = UITextBorderStyle.roundedRect
    sampleTextField.backgroundColor = #colorLiteral(red: 0.9027962089, green: 0.781386435, blue: 0.9435198903, alpha: 1)
    sampleTextField.tag=indexPath.item
    //use this if you want to clear test when you edit again
  //  sampleTextField.clearsOnBeginEditing = true
    sampleTextField.placeholder=data[indexPath.section].headerName
    headerSection=data[indexPath.section].headerName!
    cell?.addSubview(sampleTextField)

    sampleTextField.addTarget(self, action: #selector(UITextFieldDelegate.textFieldDidEndEditing(_:)), for: UIControlEvents.editingDidEnd)





    return cell!
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52973608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档