将tableview单元格中的所有文本字段附加到数组

问题描述

我希望我的快速代码在每个文本字段中获取字符,然后在调用func append时将其转换为数组。您也可以在函子中查看评论调用func时,用户获取customtv类中的所有文本字段,并将它们添加到空数组中。假定每个文本字段都包含一个整数。

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var empty = [Int]()
    var appended = UIButton()
    var tableView = UITableView()
    
    var arr = [0,1,2,3,4]
    @objc func append(){
        ///append all of the textfiels into array empty. All of the textfiels are in customtv
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
         
             appended.backgroundColor = .orange
             view.addSubview(appended)
             
             
             
             let VCframe = view.frame
             let height = VCframe.height * 0.8
             let height2 = VCframe.height * 0.2
             let widthx = VCframe.width
             appended.frame = CGRect(x: 0,y: height,width: widthx,height: height2)
             appended.addTarget(self,action: #selector(append),for: .touchDown)
             tableView.frame = CGRect(x: 10,y: 0,width: widthx - 20,height: height)
             
             tableView.delegate = self
             tableView.dataSource = self
             view.addSubview(tableView)
             
             
             tableView.register(customtv.self,forCellReuseIdentifier: "cell")
    }

 
    
    
    
    
    
    
    func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat { 118 }
    
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return arr.count
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath) as! customtv
        cell.lbl.text = "\(arr[indexPath.row])"
        return cell
    }
    
    
    
    
}
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
        print(self.frame.width)
        return view
    }()
    
    
    
    override func layoutSubviews() {
        backView.clipsToBounds = true
        backView.frame =  CGRect(x: 0,width: bounds.maxX,height: 110)
        
        
    }
    lazy var lbl : UILabel = {
        let press = UILabel(frame: CGRect(x: 0,y: 3,width: 120,height: 50))
        press.backgroundColor = .yellow
        press.text = String("1")
        
        return press
    }()
    
    lazy var txtField : UITextField = {
        let press = UITextField(frame: CGRect(x: 100,height: 50))

        press.backgroundColor = .white
        press.textAlignment = .center
        return press
    }()
    
    
    
    
    
    
    override func setSelected(_ selected: Bool,animated: Bool) {
        super.setSelected(animated,animated: true)
        addSubview(backView)
        backView.addSubview(lbl)
        backView.addSubview(txtField)
    }
}

解决方法

请尝试一下。这适用于Swift 5。

    func append() {            
        let arrayTextFields = (0..<arr.count).compactMap { (tableView.cellForRow(at: IndexPath(row: $0,section: 0)) as? customtv)?.txtField }
    }

如果要从文本字段获取Int值,则可以添加它。

       let arrayValues = arrayTextFields.compactMap { Int($0.text!) }