我有每16行2节,以及如何在tableview单元格中获取所有textfield值?要存储它,当我点击保存按钮时。
并且我已经从放置在String:AnyObject中的firebase数据库中检索了模拟数据,并在tableview上显示了它。
如何获得像文本字段的值或在tableviewCell中切换?
import UIKit
import Firebase
import FirebaseDatabaseUI
class SettingLabelTableViewController: UITableViewController, UITextFieldDelegate {
var BitArray:[String] = ["M0","M1","M2","M3","M4","M5","M6","M0","M8"
,"M9","M10","M11","M12","M13","M14","M15"]
var WordArray:[String] = ["D0","D1","D2","D3","D4","D5","D6","D0","D8"
,"D9","D10","D11","D12","D13","D14","D15"]
var DeviceKey:String?
var Ref: FIRDatabaseReference!
var dict = [String:AnyObject]()
var allCellsText = [String]()
@IBAction func SaveButton(_ sender: UIBarButtonItem)
{
/*self.Ref.setValue(dict, withCompletionBlock:
{ (error, dbref) in
})*/
}
override func viewDidLoad()
{
super.viewDidLoad()
Ref = device.child("DEVICE").child(DeviceKey!).child("SETTINGS").child("LABEL")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
Ref.observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists(){
print("not exist")
csGolbal.g_NameAry.removeAll()
self.dict.removeAll()
}
else{
self.dict.removeAll()
self.dict = (snapshot.value as? [String: AnyObject])!
/*for item in snapshot.value as! [String : String]{
csGolbal.g_NameAry.append([item.key:item.value])
}*/
}
self.tableView.reloadData()
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
if section == 0
{
return BitArray.count
}
else
{
return WordArray.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelNameCell", for: indexPath) as! LabelNameTableViewCell
cell.LabelTitle.text = BitArray[indexPath.row]
if dict.count > 0{
cell.txtName.text = dict["M"+String(indexPath.row)] as? String ?? "null"
}
return cell
}
我的tableviewCell
class LabelNameTableViewCell: UITableViewCell
{
@IBOutlet weak var LabelTitle: UILabel!
@IBOutlet weak var txtName: UITextField!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
最佳答案
你可以做这样的事情
@IBAction func saveButton(_ sender: UIBarButtonItem) {
var dict: [String:String] = [:]
for (i,bit) in bitArray.enumarate() {
let cell = tableView.cellForRow(at: IndexPath(row: i, section: 0)) as! LabelNameTableViewCell
dict[bit] = cell.txtName.text
}
for (i,word) in wordArray.enumarate() {
let cell = tableView.cellForRow(at: IndexPath(row: i, section: 1)) as! LabelNameTableViewCell
dict[word] = cell.txtName.text
}
// dict ---- "M0": "some text" ...
//then store it
}
ps。在迅速的变量和函数必须以小写字符开头,请参阅苹果标准库函数。
关于ios - 点击“保存”按钮并存储在字典中时,在表格 View 单元格中获取文本字段?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44592608/