В настоящее время я пытаюсь создать табличное представление, которое содержит данные в нескольких представлениях. Идея состоит в том, что при нажатии кнопки «Создать профиль» создается ячейка прототипа, содержащая данные со всех экранов. Пока у меня есть сохранение UserDefaults, код базового табличного представления c и ячейка прототипа. Я не уверен, как на самом деле go получить доступ ко всем сохраненным данным и сохранить их в ячейке при нажатии кнопки сохранения. Любая помощь приветствуется. Вот мой код.
Изображение потока просмотра: https://gyazo.com/7632601da0c44b61075cafdcc019ba64
Первый экран с видом таблицы:
import UIKit
class Billing: UIViewController {
var profiles = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func plusAddTaskButton(_ sender: UIBarButtonItem) {
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return profiles.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCell(withIdentifier: "profileCell", for: indexPath)
myCell.textLabel?.text = profiles[indexPath.row]
return myCell
}
}
fileprivate func delegates() {
billingTextView.delegate = self
FirstName.delegate = self
LastName.delegate = self
Email.delegate = self
Country.delegate = self
State.delegate = self
Address.delegate = self
Address2.delegate = self
City.delegate = self
PostalCode.delegate = self
PhoneNumber.delegate = self
}
Детали доставки Экран:
import Foundation
import UIKit
var billingText: String!
var firstName: String!
var lastName: String!
var email: String!
var phoneNumber: String!
var address: String!
var address2: String!
var city: String!
var postalCode: String!
var country: String!
var state: String!
class AddBilling: UIViewController,UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var billingTextView: UITextField!
@IBOutlet weak var FirstName: UITextField!
@IBOutlet weak var LastName: UITextField!
@IBOutlet weak var Email: UITextField!
@IBOutlet weak var PhoneNumber: UITextField!
@IBOutlet weak var Address: UITextField!
@IBOutlet weak var Address2: UITextField!
@IBOutlet weak var City: UITextField!
@IBOutlet weak var PostalCode: UITextField!
@IBOutlet weak var Country: UITextField!
@IBOutlet weak var State: UITextField!
@IBOutlet weak var proceedButton: UIButton!
@IBOutlet weak var proceedToSecondBillingButton: UIButton!
@IBOutlet weak var secondBillingSwitch: UISwitch!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
delegates()
billingTextView.text = defaults.string(forKey: "profileName")
FirstName.text = defaults.string(forKey: "firstName")
LastName.text = defaults.string(forKey: "lastName")
Email.text = defaults.string(forKey: "email")
PhoneNumber.text = defaults.string(forKey: "phoneNumber")
Address.text = defaults.string(forKey: "address")
Address2.text = defaults.string(forKey: "address2")
City.text = defaults.string(forKey: "city")
PostalCode.text = defaults.string(forKey: "postalCode")
Country.text = defaults.string(forKey: "country")
State.text = defaults.string(forKey: "state")
}
fileprivate func secondBillingSwitchState() {
if (defaults.object(forKey: "secondBillingSwitch") != nil) {
secondBillingSwitch.isOn = defaults.bool(forKey: "secondBillingSwitch")
}
}
@IBAction func secondBillingSwitch(_ sender: UISwitch) {
if secondBillingSwitch.isOn {
defaults.set(true, forKey: "secondBillingSwitch")
} else {
defaults.set(false, forKey: "secondBillingSwitch")
}
}
@IBAction func clear(_ sender: UIBarButtonItem) {
UserDefaults.standard.set("", forKey: "profileName")
UserDefaults.standard.set("", forKey: "firstName")
UserDefaults.standard.set("", forKey: "lastName")
UserDefaults.standard.set("", forKey: "email")
UserDefaults.standard.set("", forKey: "phoneNumber")
UserDefaults.standard.set("", forKey: "address")
UserDefaults.standard.set("", forKey: "address2")
UserDefaults.standard.set("", forKey: "city")
UserDefaults.standard.set("", forKey: "postalCode")
UserDefaults.standard.set("", forKey: "country")
UserDefaults.standard.set("", forKey: "state")
billingTextView.text = ""
FirstName.text = ""
LastName.text = ""
Email.text = ""
PhoneNumber.text = ""
Address.text = ""
Address2.text = ""
City.text = ""
PostalCode.text = ""
Country.text = ""
State.text = ""
}
@IBAction func proceedToPaymentSave(_ sender: UIButton) {
billingText = billingTextView.text!
firstName = FirstName.text!
lastName = LastName.text!
email = Email.text!
phoneNumber = PhoneNumber.text!
address = Address.text!
address2 = Address2.text!
city = City.text!
postalCode = PostalCode.text!
country = Country.text!
state = State.text!
defaults.set(billingText, forKey: "profileName")
defaults.set(firstName, forKey: "firstName")
defaults.set(lastName, forKey: "lastName")
defaults.set(email, forKey: "email")
defaults.set(phoneNumber, forKey: "phoneNumber")
defaults.set(address, forKey: "address")
defaults.set(address2, forKey: "address2")
defaults.set(city, forKey: "city")
defaults.set(postalCode, forKey: "postalCode")
defaults.set(country, forKey: "country")
defaults.set(state, forKey: "state")
}
fileprivate func delegates() {
billingTextView.delegate = self
FirstName.delegate = self
LastName.delegate = self
Email.delegate = self
Country.delegate = self
State.delegate = self
Address.delegate = self
Address2.delegate = self
City.delegate = self
PostalCode.delegate = self
PhoneNumber.delegate = self
}
}
Экран платежных данных:
import UIKit
import Foundation
var firstNameBilling: String!
var lastNameBilling: String!
var addressBilling: String!
var address2Billing: String!
var cityBilling: String!
var postalCodeBilling: String!
var countryBilling: String!
var stateBilling: String!
class AddBilling2: UIViewController,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource {
@IBOutlet weak var FirstNameBilling: UITextField!
@IBOutlet weak var LastNameBilling: UITextField!
@IBOutlet weak var AddressBilling: UITextField!
@IBOutlet weak var AddressBilling2: UITextField!
@IBOutlet weak var CityBilling: UITextField!
@IBOutlet weak var CountryBilling: UITextField!
@IBOutlet weak var StateBilling: UITextField!
@IBOutlet weak var PostalCodeBilling: UITextField!
@IBOutlet weak var proceedButton: UIButton!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
delegates()
FirstNameBilling.text = defaults.string(forKey: "firstNameBilling")
LastNameBilling.text = defaults.string(forKey: "lastNameBilling")
AddressBilling.text = defaults.string(forKey: "addressBilling")
AddressBilling2.text = defaults.string(forKey: "address2Billing")
CityBilling.text = defaults.string(forKey: "cityBilling")
PostalCodeBilling.text = defaults.string(forKey: "postalCodeBilling")
CountryBilling.text = defaults.string(forKey: "countryBilling")
StateBilling.text = defaults.string(forKey: "stateBilling")
}
@IBAction func proceedToPayment(_ sender: UIButton) {
firstNameBilling = FirstNameBilling.text!
lastNameBilling = LastNameBilling.text!
addressBilling = AddressBilling.text!
address2Billing = AddressBilling2.text!
cityBilling = CityBilling.text!
postalCodeBilling = PostalCodeBilling.text!
countryBilling = CountryBilling.text!
stateBilling = StateBilling.text!
defaults.set(firstNameBilling, forKey: "firstNameBilling")
defaults.set(lastNameBilling, forKey: "lastNameBilling")
defaults.set(addressBilling, forKey: "addressBilling")
defaults.set(address2Billing, forKey: "address2Billing")
defaults.set(cityBilling, forKey: "cityBilling")
defaults.set(postalCodeBilling, forKey: "postalCodeBilling")
defaults.set(countryBilling, forKey: "countryBilling")
defaults.set(stateBilling, forKey: "stateBilling")
}
@IBAction func clear(_ sender: UIBarButtonItem) {
UserDefaults.standard.set("", forKey: "firstNameBilling")
UserDefaults.standard.set("", forKey: "lastNameBilling")
UserDefaults.standard.set("", forKey: "addressBilling")
UserDefaults.standard.set("", forKey: "address2Billing")
UserDefaults.standard.set("", forKey: "cityBilling")
UserDefaults.standard.set("", forKey: "postalCodeBilling")
UserDefaults.standard.set("", forKey: "countryBilling")
UserDefaults.standard.set("", forKey: "stateBilling")
FirstNameBilling.text = ""
LastNameBilling.text = ""
AddressBilling.text = ""
AddressBilling2.text = ""
CityBilling.text = ""
PostalCodeBilling.text = ""
CountryBilling.text = ""
StateBilling.text = ""
}
fileprivate func delegates() {
FirstNameBilling.delegate = self
LastNameBilling.delegate = self
CountryBilling.delegate = self
StateBilling.delegate = self
AddressBilling.delegate = self
AddressBilling2.delegate = self
CityBilling.delegate = self
PostalCodeBilling.delegate = self
}
}
Экран платежных данных:
import UIKit
var cardHolder: String!
var cardNumber: String!
var expirationDate: String!
var cvc: String!
class AddCreditCard: UIViewController,UITextFieldDelegate {
@IBOutlet weak var CardName: UITextField!
@IBOutlet weak var CardNumber: UITextField!
@IBOutlet weak var CardExpirationDate: UITextField!
@IBOutlet weak var CardCVC: UITextField!
@IBOutlet weak var createProfileButton: UIButton!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
delegate()
CardName.text = defaults.string(forKey: "cardHolder")
CardNumber.text = defaults.string(forKey: "cardNumber")
CardExpirationDate.text = defaults.string(forKey: "expirationDate")
CardCVC.text = defaults.string(forKey: "cvc")
}
@IBAction func clear(_ sender: UIBarButtonItem) {
UserDefaults.standard.set("", forKey: "cardHolder")
UserDefaults.standard.set("", forKey: "cardNumber")
UserDefaults.standard.set("", forKey: "expirationDate")
UserDefaults.standard.set("", forKey: "cvc")
CardName.text = ""
CardNumber.text = ""
CardExpirationDate.text = ""
CardCVC.text = ""
}
@IBAction func createProfileButton(_ sender: UIButton) {
cardHolder = CardName.text!
cardNumber = CardNumber.text!
expirationDate = CardExpirationDate.text!
cvc = CardCVC.text!
defaults.set(cardHolder, forKey: "cardHolder")
defaults.set(CardNumber, forKey: "cardNumber")
defaults.set(expirationDate, forKey: "expirationDate")
defaults.set(cvc, forKey: "cvc")
}
fileprivate func delegate() {
CardName.delegate = self
CardExpirationDate.delegate = self
}
}