当您尝试删除项目时,我的应用程序崩溃,并且它不显示所有三个警报字段?



图像

更新的代码xxxxxxxxxxxxxxxxxxxx得到错误,然后当我运行时,我得到了致命的错误,不确定为什么会发生这种情况,但这是在你的人帮助后更新的代码。请告诉我xxxxxxxxxxxxxxxxxxxx

import UIKit
let defaults = UserDefaults(suiteName: "com.Saving.Data")
struct Product: Codable {
var title: String
var price: String
var salePrice: String
}
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var items: [(item: String?, price: String?, salesPrice: String?)] = []
override func viewDidLoad() {
super.viewDidLoad()
getData()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
getData()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(true)
storeData()
}
override var prefersStatusBarHidden: Bool {
return true
}
@IBAction func addButtonTapped(_ sender: Any) {
let alert = UIAlertController(title: "Product Information", message: nil, preferredStyle: .alert)
alert.addTextField { (itemTF) in
itemTF.placeholder = "Item"
}
alert.addTextField { (textField) in
textField.placeholder = "Price"
}
alert.addTextField { (textField) in
textField.placeholder = "Sale Price"
}
let action = UIAlertAction(title: "Add", style: .default) { (_) in
var product : (item: String, price: String, salesPrice: String) = ("","","")
if let textField1 = alert.textFields?[0], let text = textField1.text {
print(text)
product.item = text
}
if let textField2 = alert.textFields?[1], let text = textField2.text {
print(text)
product.price = text
}
if let textField3 = alert.textFields?[2], let text = textField3.text {
print(text)
product.salesPrice = text
}
self.add(product)
}
alert.addAction(action)
present(alert, animated: true)
storeData()
}
func add(_ product: (item: String, price: String, salesPrice: String)) {
let index = 0
items.insert(product, at: index)
let indexPath = IndexPath(row: index, section: 0)
tableView.insertRows(at: [indexPath], with: .left)
storeData()
}

func storeData() {
if let data = try? PropertyListEncoder().encode(items) {
defaults?.set(data, forKey: "savedData")
}
}
func getData() {
if let data = defaults?.data(forKey: "savedData") {
items = try! PropertyListDecoder().decode([Product].self, from: data)
}
}

}
extension ViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let product = items[indexPath.row]
cell.textLabel?.text = product.item
print(product.price ?? "")
print(product.salesPrice ?? "")
cell.contentView.backgroundColor = UIColor(red:0.92, green:0.92, blue:0.92, alpha:1.0)
cell.textLabel?.textColor = UIColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
tableView.separatorColor = UIColor(red:0.92, green:0.92, blue:0.92, alpha:1.0)

return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
items.remove(at: indexPath.row)
tableView.reloadData()
storeData()
}
}

要显示产品的所有信息,如名称、价格、salePrice,您必须创建结构或者元组。它需要确认可编码协议才能保存到用户默认值

struct Product: Codable {
var title: String
var price: String
var salePrice: String
}

然后,您必须创建一个自定义的UITableViewCell,其中包含多个(这次是3个(UI标签和适当的自动布局约束以显示产品的所有信息。

这是的完整代码

import UIKit
struct Product: Codable {
var title: String
var price: String
var salePrice: String
}
let defaults = UserDefaults(suiteName: "savedData")
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var items = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addButtonPressed))
tableView.delegate = self
tableView.dataSource = self
getData()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
getData()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
storeData()
}
@objc func addButtonPressed() {
let alert = UIAlertController(title: "Product Information", message: nil, preferredStyle: .alert)
alert.addTextField { (itemTF) in
itemTF.placeholder = "Item"
}
alert.addTextField { (textField) in
textField.placeholder = "Price"
}
alert.addTextField { (textField) in
textField.placeholder = "Sale Price"
}
let addAction = UIAlertAction(title: "Add", style: .default) { (_) in
let firstTextField = alert.textFields![0] as UITextField
let secondTextField = alert.textFields![1] as UITextField
let thirdTextField = alert.textFields![2] as UITextField
let product = Product(title: firstTextField.text!, price: secondTextField.text!, salePrice: thirdTextField.text!)
self.items.append(product)
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(addAction)
alert.addAction(cancelAction)
present(alert, animated: true)
}
func add(_ product: Product) {
let index = 0
items.insert(product, at: index)
let indexPath = IndexPath(row: index, section: 0)
tableView.insertRows(at: [indexPath], with: .left)
storeData()
}

func storeData() {
if let data = try? PropertyListEncoder().encode(items) {
defaults?.set(data, forKey: "savedData")
}
}
func getData() {
if let data = defaults?.data(forKey: "savedData") {
items = try! PropertyListDecoder().decode([Product].self, from: data)
}
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.data = items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
items.remove(at: indexPath.row)
self.tableView.reloadData()
storeData()
}
}
class CustomCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var salePriceLabel: UILabel!
var data: Product? {
didSet {
guard let data = data else {return}
titleLabel.text = data.title
priceLabel.text = data.price
salePriceLabel.text = data.salePrice
}
}
override func awakeFromNib() {
super.awakeFromNib()
}
}

您可以在alertView中添加3个文本字段。

代码:

@IBAction func addButtonTapped(_ sender: Any) {
let alert = UIAlertController(title: "Product Information", message: nil, preferredStyle: .alert)
alert.addTextField { (itemTF) in
itemTF.placeholder = "Item"
}
alert.addTextField { (textField) in
textField.placeholder = "Price"
}
alert.addTextField { (textField) in
textField.placeholder = "Sale Price"
}
let action = UIAlertAction(title: "Add", style: .default) { (_) in
var product : (item: String, price: String, salesPrice: String) = ("","","")
if let textField1 = alert.textFields?[0], let text = textField1.text  as? String {
print(text)
product.item = text
}
if let textField2 = alert.textFields?[1], let text = textField2.text  as? String {
print(text)
product.price = text
}
if let textField3 = alert.textFields?[2], let text = textField3.text as? String {
print(text)
product.salesPrice = text
}
}
self.add(product)
alert.addAction(action)
present(alert, animated: true)
}

func add(_ product: (item: String, price: String, salesPrice: String)) {
let index = 0
items.insert(product, at: index)
let indexPath = IndexPath(row: index, section: 0)
tableView.insertRows(at: [indexPath], with: .left)
storeData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let product = items[indexPath.row]
cell.textLabel?.text = product.item
print(product.price)
print(product.salesPrice)
cell.contentView.backgroundColor = UIColor(red:0.92, green:0.92, blue:0.92, alpha:1.0)
cell.textLabel?.textColor = UIColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
tableView.separatorColor = UIColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
return cell
}

最新更新