gpt4 book ai didi

ios - 将多个项目放入 TableView 单元格时出现问题

转载 作者:行者123 更新时间:2023-11-29 05:15:56 27 4
gpt4 key购买 nike

我试图在单个表格 View 单元格中获取三个项目,就像图片一样。

我希望应用程序看起来与图片完全一样。

Image(CLICK ME) What I want it to look like

我尝试过一百万种不同的方法,但似乎无法找出正确的方法,应用程序现在完全崩溃了。

需要一些帮助,因为我是 Swift 的初学者,我仍在学习,但由于某种原因,这是一个挑战。

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")
}
}

最佳答案

    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(configurationHandler: { (itemTextField) -> Void in
itemTextField.placeholder = "Item"
})
alert.addTextField(configurationHandler: { (priceTextField) -> Void in
priceTextField.placeholder = "Price"
})
alert.addTextField(configurationHandler: { (salePriceTextField) -> Void in
salePriceTextField.placeholder = "Sale Price"
})



alert.addAction(UIAlertAction(title: "Add", style: .default) { (action) 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)
})

self.present(alert, animated: true, completion: nil)

storeData()

}

func addProduct(_ product: Product) {
items.append(product)
tableView.reloadData()
storeData()
}

//STORE DATA
func storeData() {
UserDefaultUtil.saveData(products: items)
}

func getData() {
if let data = UserDefaults.standard.data(forKey: "savedData"), let unarchivedObject = NSKeyedUnarchiver.unarchiveObject(with: data) as? [Product] {
items = unarchivedObject
}
}

}

//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 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")
}
}

关于ios - 将多个项目放入 TableView 单元格时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59187166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com