Tableview 单元格将字符串随机放置在空文本视图中

Tableview cell randomly place string in empty textview

在我下面的 swift 代码中,按下添加按钮以添加另一个 tableview 单元格。问题是在添加的第六个 tableview 单元格中,数字 1 只是随机出现,每次添加另一个单元格时都会重复此问题。你可以在下面的 gif 中看到我在说什么。我在答案中寻找的只是在文本视图中添加没有任何内容的单元格。

import UIKit
/*
 
 */
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    
    var addBTN = UIButton()
    var arr = [nil] as [Any?]
    var currentIndex = 0
    var arrayValues = [Int]()
    var index = Int()
    
    
    
    
    var pic = UIImageView()
    
    var cellCount = 0
    var tableview = UITableView()
    
    
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 118
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTv
        
        
        return cell
    }
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [addBTN].forEach{
            [=10=].translatesAutoresizingMaskIntoConstraints = false
            view.addSubview([=10=])
        }
        tableview.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.8)
        addBTN.frame = CGRect(x: view.frame.width / 2, y: view.frame.height * 0.8, width: view.frame.width / 2  , height: view.frame.height / 5)
        addBTN.backgroundColor = .systemTeal
        
        view.addSubview(tableview)
        
        
        tableview.register(CustomTv.self, forCellReuseIdentifier: "cell")
        tableview.delegate = self
        tableview.dataSource = self
        
        pic.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height )
        
        
        addBTN.addTarget(self, action: #selector(newCell), for: .touchDown)
        
        
        addBTN.setTitle("New cell", for: .normal)
    }
    
    @objc func newCell(){
        cellCount += 1 // update the value
        arr.append(1)
        tableview.beginUpdates()
        tableview.insertRows(at: [IndexPath(row: arr.count - 1, section: 0)], with: .automatic) /// animate the insertion
        tableview.endUpdates()
        
    }
    
    
}


class CustomTv: UITableViewCell {
    
    lazy var backView : UIView = {
        let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width  , height: 110))
        view.backgroundColor = .green
        
        return view
    }()
    
    
    
    
    
    lazy var txt : UITextField = {
        let btn = UITextField(frame: CGRect(x: 150-25, y: 60, width: 100 , height: 50))
        btn.backgroundColor = .orange
        return btn
        
    }()
    override func layoutSubviews() {
        backView.clipsToBounds = true
        backView.frame =  CGRect(x: 0, y: 6, width: bounds.maxX  , height: 110)

        addSubview(backView)
        
        backView.addSubview(txt)
        
        
        
        
        
        
        
    }
    
}

在单元格内实现 prepareForReuse 方法 class 并在方法内清除文本字段

 override func prepareForReuse() {
        // clear the text field
    }