我有一个ViewController,用户可以在4个UITextField中输入数据,由UIPickerViews填充,当用户按下保存按钮时,数据应该更新相应的自定义UITableViewCell中的相应UILabel。
请参阅以下链接。
TableView and Input ViewControllers with arrows showing corresponding UILabels and UITextFields
目前,通过提供的代码,自定义UITableViewCell中的所有UILabel都显示相同的数据。
请参阅以下链接。
Current TableViewCell output in Simulator
我怀疑我在RunFeedViewController
中遇到了这个问题public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "runFeedCell1", for: indexPath) as! Mountain1TableViewCell
cell.mountain1RunTypeLabel?.text = runFeedList[indexPath.item]
cell.mountain1RunNameLabel?.text = runFeedList[indexPath.item]
cell.mountain1RunSnowLabel?.text = runFeedList[indexPath.item]
cell.mountain1RunDateLabel?.text = runFeedList[indexPath.item]
return (cell)
}
和/或Mountain1RunInputViewController中的这部分
@IBAction func saveMountain1Run(_ sender: Any) {
if (mountain1RunDateTextField.text != "")
{
runFeedList.append(mountain1RunDateTextField.text!)
mountain1RunDateTextField.text = ""
UserDefaults.standard.set(runFeedList, forKey: "runFeedList")
}
}
我错过了使所有4个UILabel与其对应的UITextField一起使用?。
以下是带有UITableView的RunFeedViewController的代码。
import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
var runFeedList = [String]()
class RunFeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var runFeedListTable: UITableView!
var ref: DatabaseReference?
var databaseHandle: DatabaseHandle?
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (runFeedList.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "runFeedCell1", for: indexPath) as! Mountain1TableViewCell
cell.mountain1RunTypeLabel?.text = runFeedList[indexPath.item]
cell.mountain1RunNameLabel?.text = runFeedList[indexPath.item]
cell.mountain1RunSnowLabel?.text = runFeedList[indexPath.item]
cell.mountain1RunDateLabel?.text = runFeedList[indexPath.item]
return (cell)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath){
if editingStyle == UITableViewCellEditingStyle.delete
{
runFeedList.remove(at: indexPath.row)
UserDefaults.standard.set(runFeedList, forKey: "runFeedList")
runFeedListTable.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
runFeedListTable.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.object(forKey: "runFeedList") != nil
{
runFeedList = UserDefaults.standard.object(forKey: "runFeedList") as! [String]
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func logOutAction(_ sender: Any) {
if Auth.auth().currentUser != nil {
do {
try Auth.auth().signOut()
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignUp")
present(vc, animated: true, completion: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
}
以下是Mountain1RunInputViewController的代码,用户在4 UITextFields中输入数据。
import UIKit
var mountain1RunTypeArray = ["1", "2", "3", "4"]
var mountain1RunNameArray1 = ["1A","1B","1C", "1D"]
var mountain1RunNameArray2 = ["2A","2B","2C", "2D"]
var mountain1RunNameArray3 = ["3A","3B","3C", "3D"]
var mountain1RunNameArray4 = ["4A","4B","4C", "4D"]
var mountain1RunSnowArray = ["Good", "Bad"]
var mountain1SelectedItemsArray = [String]()
var mountain1RunTypeTextField: String = ""
var mountain1RunNameTextField: String = ""
var mountain1RunSnowTextField: String = ""
var mountain1RunDateTextField: String = ""
class Mountain1RunInputViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate{
var pickerView1 = UIPickerView()
var pickerView2 = UIPickerView()
var pickerView3 = UIPickerView()
var pickerView4 = UIDatePicker()
@IBOutlet weak var mountain1RunTypeTextField: UITextField!
@IBOutlet weak var mountain1RunNameTextField: UITextField!
@IBOutlet weak var mountain1RunSnowTextField: UITextField!
@IBOutlet weak var mountain1RunDateTextField: UITextField!
@IBAction func saveMountain1Run(_ sender: Any) {
if (mountain1RunDateTextField.text != "")
{
runFeedList.append(mountain1RunDateTextField.text!)
mountain1RunDateTextField.text = ""
UserDefaults.standard.set(runFeedList, forKey: "runFeedList")
}
}
override func viewDidLoad() {
super.viewDidLoad()
createToolbar()
pickerView1 = UIPickerView()
pickerView2 = UIPickerView()
pickerView3 = UIPickerView()
pickerView4 = UIDatePicker()
pickerView1.delegate = self
pickerView1.dataSource = self
pickerView2.delegate = self
pickerView2.dataSource = self
pickerView3.delegate = self
pickerView3.dataSource = self
pickerView1.tag = 0
pickerView2.tag = 1
pickerView3.tag = 2
pickerView4.tag = 3
self.mountain1RunTypeTextField.inputView = self.pickerView1;
self.mountain1RunNameTextField.inputView = self.pickerView2;
self.mountain1RunSnowTextField.inputView = self.pickerView3;
self.mountain1RunDateTextField.inputView = self.pickerView4;
pickerView4.addTarget(self, action: #selector(datePickerValueChanged(sender:)), for: .valueChanged)
pickerView4.datePickerMode = UIDatePickerMode.dateAndTime
}
func createToolbar() {
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let cancelButton1 = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Mountain1RunInputViewController.cancelPicker1))
let cancelButton2 = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Mountain1RunInputViewController.cancelPicker2))
let cancelButton3 = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Mountain1RunInputViewController.cancelPicker3))
let cancelButton4 = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(Mountain1RunInputViewController.cancelPicker4))
// Tool bar for 1st UIPickerView
let toolBar1 = UIToolbar()
toolBar1.sizeToFit()
toolBar1.barTintColor = .black
toolBar1.tintColor = .white
let doneButton1 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(Mountain1RunInputViewController.doneButton1(sender:)))
toolBar1.setItems([cancelButton1,spaceButton,doneButton1], animated: false)
toolBar1.isUserInteractionEnabled = true
mountain1RunTypeTextField.inputAccessoryView = toolBar1
// Tool bar for 2nd UIPickerView
let toolBar2 = UIToolbar()
toolBar2.sizeToFit()
toolBar2.barTintColor = .black
toolBar2.tintColor = .white
let doneButton2 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(Mountain1RunInputViewController.doneButton2(sender:)))
toolBar2.setItems([cancelButton2,spaceButton,doneButton2], animated: false)
toolBar2.isUserInteractionEnabled = true
mountain1RunNameTextField.inputAccessoryView = toolBar2
// Tool bar for 3rd UIPickerView
let toolBar3 = UIToolbar()
toolBar3.sizeToFit()
toolBar3.barTintColor = .black
toolBar3.tintColor = .white
let doneButton3 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(Mountain1RunInputViewController.doneButton3(sender:)))
toolBar3.setItems([cancelButton3,spaceButton,doneButton3], animated: false)
toolBar3.isUserInteractionEnabled = true
mountain1RunSnowTextField.inputAccessoryView = toolBar3
// Tool bar for UIDatePicker
let toolBar4 = UIToolbar()
toolBar4.sizeToFit()
toolBar4.barTintColor = .black
toolBar4.tintColor = .white
let doneButton4 = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(Mountain1RunInputViewController.doneButton4))
toolBar4.setItems([cancelButton4,spaceButton,doneButton4], animated: false)
toolBar4.isUserInteractionEnabled = true
mountain1RunDateTextField.inputAccessoryView = toolBar4
}
func cancelPicker1(){
self.view.endEditing(true)
}
func cancelPicker2(){
self.view.endEditing(true)
}
func cancelPicker3(){
self.view.endEditing(true)
}
func cancelPicker4(){
self.view.endEditing(true)
}
// Done button for 1st UIPickerView
func doneButton1(sender: AnyObject) {
let row1 = pickerView1.selectedRow(inComponent: 0);
pickerView(pickerView1, didSelectRow: row1, inComponent:0)
view.endEditing(true)
}
// Done button for 2nd UIPickerView
func doneButton2(sender: AnyObject) {
let row2 = pickerView2.selectedRow(inComponent: 0);
pickerView(pickerView2, didSelectRow: row2, inComponent:0)
view.endEditing(true)
}
// Done button for 3rd UIPickerView
func doneButton3(sender: AnyObject) {
let row3 = pickerView3.selectedRow(inComponent: 0);
pickerView(pickerView3, didSelectRow: row3, inComponent:0)
view.endEditing(true)
}
// Done button for UIDatePicker
func doneButton4(){
let formatter = DateFormatter()
formatter.dateStyle = DateFormatter.Style.long
formatter.timeStyle = DateFormatter.Style.short
mountain1RunDateTextField.text = formatter.string(from: pickerView4.date)
self.view.endEditing(true)
}
func dismissKeyboard() {
self.view.endEditing(true)
}
func datePickerValueChanged(sender:UIDatePicker) {
let dateFormatter = DateFormatter()
pickerView4.datePickerMode = UIDatePickerMode.dateAndTime
mountain1RunDateTextField.text = dateFormatter.string(from: sender.date)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return mountain1RunTypeArray.count
} else if pickerView.tag == 1 {
return mountain1SelectedItemsArray.count
} else if pickerView.tag == 2 {
return mountain1RunSnowArray.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 {
return mountain1RunTypeArray[row]
} else if pickerView.tag == 1 {
return mountain1SelectedItemsArray[row]
} else if pickerView.tag == 2 {
return mountain1RunSnowArray[row]
}
return ""
}
var selectedRow: Int = 0
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedRow = row
if pickerView == pickerView1 {
mountain1RunTypeTextField.text = mountain1RunTypeArray[row]
switch row {
case 0:
mountain1SelectedItemsArray = mountain1RunNameArray1;
case 1:
mountain1SelectedItemsArray = mountain1RunNameArray2
case 2:
mountain1SelectedItemsArray = mountain1RunNameArray3
case 3:
mountain1SelectedItemsArray = mountain1RunNameArray4
default:
mountain1SelectedItemsArray = []
}
pickerView2.reloadAllComponents()
} else if pickerView == pickerView2 {
let item = mountain1SelectedItemsArray[row]
if mountain1SelectedItemsArray == mountain1RunNameArray1 {
mountain1RunNameTextField.text = item
} else if mountain1SelectedItemsArray == mountain1RunNameArray2 {
mountain1RunNameTextField.text = item
} else if mountain1SelectedItemsArray == mountain1RunNameArray3 {
mountain1RunNameTextField.text = item
} else if mountain1SelectedItemsArray == mountain1RunNameArray4 {
mountain1RunNameTextField.text = item
}
}
if pickerView.tag == 2 {
mountain1RunSnowTextField.text = mountain1RunSnowArray[row]
}
}
}
以下是相应原型单元的代码。
import UIKit
class Mountain1TableViewCell: UITableViewCell {
@IBOutlet weak var mountain1RunTypeLabel: UILabel!
@IBOutlet weak var mountain1RunNameLabel: UILabel!
@IBOutlet weak var mountain1RunSnowLabel: UILabel!
@IBOutlet weak var mountain1RunDateLabel: UILabel!
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
}
}