我不知道为什么我的应用崩溃了,我有几个问题
1。为什么当我尝试从tableview删除项目时崩溃,也一旦崩溃,它就会删除它下面的所有单元格
2。我的警报具有3个输入字段,但tableviewcell中仅第一个显示,我希望它显示项目价格和销售价格。当前仅显示项目。
如果有人可以帮助我解决此问题,将不胜感激
import UIKit
let defaults = UserDefaults(suiteName: "com.Saving.Data")
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var items = [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"
}
_ = UIAlertController(title: "", message: nil, preferredStyle: .alert)
alert.addTextField { (priceTF) in
priceTF.placeholder = "Price"
}
_ = UIAlertController(title: "", message: nil, preferredStyle: .alert)
alert.addTextField { (saleTF) in
saleTF.placeholder = "Sale Price"
}
let action = UIAlertAction(title: "Add", style: .default) { (_) in
guard let product = alert.textFields?.first?.text else { return }
print(product)
self.add(product)
}
alert.addAction(action)
present(alert, animated: true)
storeData()
}
func add(_ product: 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() {
defaults?.set(items, forKey: "savedData")
defaults?.synchronize()
}
func getData() {
let data = defaults?.value(forKey: "savedData")
if data != nil {
items = data as! [String]
} else{}
}
}
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
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
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
items.remove(at: indexPath.row)
storeData()
}
}
您可以在alertView中添加3个文本字段。
code:
@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
guard let product = alert.textFields?.first?.text else { return }
print(product)
self.add(product)
}
alert.addAction(action)
present(alert, animated: true)
}