将 tableview 单元格中的所有文本字段附加到数组
append all textfields in tableview cells to array
我希望我的 swift 代码获取每个文本字段中的字符,然后在调用 func append 时将其转换为数组。您也可以在 func 中看到注释。调用 func 时,用户将获取 class customtv 中的所有文本字段,并将它们添加到空数组中。假设每个文本字段都包含一个 int.
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, y: 6, 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, y: 3, width: 120 , 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: [=10=], section: 0)) as? customtv)?.txtField }
}
如果你想从文本字段中获取 Int 值,你可以添加这个。
let arrayValues = arrayTextFields.compactMap { Int([=11=].text!) }
我希望我的 swift 代码获取每个文本字段中的字符,然后在调用 func append 时将其转换为数组。您也可以在 func 中看到注释。调用 func 时,用户将获取 class customtv 中的所有文本字段,并将它们添加到空数组中。假设每个文本字段都包含一个 int.
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, y: 6, 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, y: 3, width: 120 , 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: [=10=], section: 0)) as? customtv)?.txtField }
}
如果你想从文本字段中获取 Int 值,你可以添加这个。
let arrayValues = arrayTextFields.compactMap { Int([=11=].text!) }