我希望打印出在OrderNumberLabel中添加到我的表视图中的行数,例如,如果有人向表视图中添加了4项,我希望它计算并打印OrderNumberLabel中的数字4。这能做到吗?我尝试了几种方法,但是没有什么是对我有用的,任何帮助都将是非常感谢的!
import UIKit
import SafariServices
class ViewController: UIViewController, UIAdaptivePresentationControllerDelegate, UITextFieldDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var OrderNumberLabel: RoundedLabel2!
@IBOutlet weak var CostLabel: RoundedLabel2!
@IBOutlet weak var ProfitsLabel: RoundedLabel2!
@IBOutlet weak var itemTextField: UITextField!
@IBOutlet weak var pricesTextField: UITextField!
@IBOutlet weak var salepriceTextField: UITextField!
var items: [ItemRow] = []
struct ItemRow: Codable {
var item: String
var price: Double
var salePrice: Double
private enum CodingKeys : String, CodingKey {case item, price, salePrice}
var profit : Double {
return salePrice - price
}
var cost : Double {
return price
}
}
override func viewDidLoad() {
super.viewDidLoad()
load()
updateProfitLabel()
updateCostLabel()
}
override var prefersStatusBarHidden: Bool {
return true
}
func save(){
if let data = try? PropertyListEncoder().encode(items) {
UserDefaults.standard.set(data, forKey: "Saved")
}
}
func load(){
let defaults = UserDefaults.standard
if let data = defaults.data(forKey: "Saved") {
items = try! PropertyListDecoder().decode([ItemRow].self, from: data)
}
}
@IBAction func addButtonTapped(_ sender: Any) {
insertNewItems()
save()
}
func insertNewItems() {
let price = Double(pricesTextField.text!) ?? 0.0
let salePrice = Double(salepriceTextField.text!) ?? 0.0
let newVideoRow = ItemRow(item: itemTextField.text!, price: price, salePrice: salePrice)
items.append(newVideoRow)
let indexPath = IndexPath(row: items.count - 1, section: 0)
tableView.insertRows(at: [indexPath], with: .automatic)
updateProfitLabel()
updateCostLabel()
itemTextField.text = ""
pricesTextField.text = ""
salepriceTextField.text = ""
view.endEditing(true)
}
func updateProfitLabel() {
let profitTotal = items.map{$0.profit}.reduce(0.0, +)
ProfitsLabel.text = String(format: "%.2f", profitTotal)
}
func updateCostLabel() {
let costTotal = items.map{$0.cost}.reduce(0.0, +)
CostLabel.text = String(format: "%.2f", costTotal)
}
}
extension ViewController: UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let ItemTitle = items[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell") as! ItemCell
cell.itemLabel.text = ItemTitle.item
cell.priceLabel.text = String(format: "%.2f", ItemTitle.price)
cell.saleLabel.text = String(format: "%.2f", ItemTitle.salePrice)
tableView.separatorColor = UIColor(red:0, green:0, blue:0, alpha:1.0)
cell.contentView.backgroundColor = UIColor(red:0.13, green:0.13, blue:0.13, alpha:1.0)
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
save()
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。