我试图在一个单一的桌面单元格中获得三个项目,就像图片一样。
我希望这个应用程序看起来和图片完全一样。 https://i.stack.imgur.com/EE5E3.jpg 我试着做了一百万个不同的方式,但似乎无法找到正确的方法,应用程序现在完全崩溃。
需要一些帮助,因为我是一个初学者迅速,我仍然在学习,但由于某些原因,这一直是一个挑战。
import UIKit
class ViewController: UIViewController, UIAdaptivePresentationControllerDelegate {
@IBOutlet weak var tableView: UITableView!
var items:[Product]? = []
// VIEW LOAD
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 13.0, *) {
self.isModalInPresentation = true
}
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
}
// ADD ITEMS
@IBAction func addButtonTapped(_ sender: Any) {
let alert = UIAlertController(title: "Product Information", message: nil, preferredStyle: .alert)
alert.addTextField { (itemTextField) in
itemTextField.placeholder = "Item"
}
alert.addTextField { (priceTextField) in
priceTextField.placeholder = "Price"
}
alert.addTextField { (salePriceTextField) in
salePriceTextField.placeholder = "Sale Price"
}
let action = UIAlertAction(title: "Add", style: .default) { (_) in
let item = alert.textFields?[0].text ?? ""
let price = alert.textFields?[1].text ?? ""
let salesPrice = alert.textFields?[2].text ?? ""
let product = Product(item: item, price: price, salesPrice: salesPrice)
self.addProduct(product)
}
alert.addAction(action)
present(alert, animated: true)
storeData()
}
func addProduct(_ product: Product) {
let index = 0
items?.insert(product, at: index)
let indexPath = IndexPath(row: index, section: 0)
tableView.insertRows(at: [indexPath], with: .left)
storeData()
}
//STORE DATA
func storeData() {
UserDefaultUtil.saveData(products: items)
}
func getData() {
items = UserDefaultUtil.loadProducts()
}
}
//EXTENSION
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!) \(product.price) \(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
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
items?.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
storeData()
}
}
class UserDefaultUtil {
private static let Key = "savedData"
private static func archivePeople(people : [Product]) -> NSData {
return NSKeyedArchiver.archivedData(withRootObject: people as NSArray) as NSData
}
static func loadProducts() -> [Product]? {
if let unarchivedObject = UserDefaults.standard.object(forKey: Key) as? Data {
return NSKeyedUnarchiver.unarchiveObject(with: unarchivedObject as Data) as? [Product]
}
return nil
}
static func saveData(products : [Product]?) {
let archivedObject = archivePeople(people: products!)
UserDefaults.standard.set(archivedObject, forKey: Key)
UserDefaults.standard.synchronize()
}
}
class Product: NSObject, NSCoding {
var item: String?
var price: String?
var salesPrice: String?
required init(item:String, price:String, salesPrice: String) {
self.item = item
self.price = price
self.salesPrice = salesPrice
}
required init(coder aDecoder: NSCoder) {
self.item = aDecoder.decodeObject(forKey: "item") as? String
self.price = aDecoder.decodeObject(forKey: "price") as? String
self.salesPrice = aDecoder.decodeObject(forKey: "salesPrice") as? String
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(item, forKey: "item")
aCoder.encode(price, forKey: "price")
aCoder.encode(salesPrice, forKey: "salesPrice")
}
}
我猜它在这里崩溃了:let archivedObject = archivePeople(people: products!)...检查以确保您有一个有效的数组。删除对storeData()在.的末尾addButtonTapped.
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。