ios - 当您尝试删除一个项目时我的应用程序崩溃并且它不显示所有三个警报字段?
问题描述
更新的代码 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 出现错误,然后当我运行时出现致命错误,不知道为什么会这样,但这是在你们帮助后更新的代码。请告诉我xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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()
}
}
解决方案
要显示产品的所有信息,如名称、价格、销售价格,您必须创建一个Class或Structure或Tuple。它需要确认Codable Protocol 才能保存到UserDefaults。
struct Product: Codable {
var title: String
var price: String
var salePrice: String
}
然后,您必须使用几个(这次是 3 个)UILabel和适当的AutoLayoutConstraints创建一个自定义UITableViewCell来显示Product的所有信息。
这是完整的代码
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()
}
}
推荐阅读
- python - 在雅可比矩阵计算期间避免警告
- html - 为什么我的 Bootstrap 4 Carousel 不能在 Codeply 上运行?
- python - 如何在使用 Selenium 将数据抓取到 Csv 期间处理损坏的字符类型
- java - 使用 lambda Java 将列表创建更改为 json
- wordpress - 使用托管在 WordPress 页面中的 Create-React-App 进行客户端路由时,我需要哪些 .htaccess 规则?
- python - 如何在简单的图表上实现“播放头”类型的功能?
- computer-science - 给定一个类似“树”的数据结构,打印出从叶子到根的所有路径
- c# - If 语句覆盖先前的 Setter
- dependency-injection - 如何在 Angular 9 中将对话框注入到服务中
- angular - 试图附加多个图像,但从角度数组中只得到一个