[英]Having issues printing the count of tableview
I want to print out the number of rows that have been added to my tableview in OrderNumberLabel, for example if someone has added 4 items to the table view i want it to calculate and print out the number 4 in the OrderNumberLabel.我想在 OrderNumberLabel 中打印出已添加到我的 tableview 的行数,例如,如果有人向 table view 添加了 4 个项目,我希望它计算并打印出 OrderNumberLabel 中的数字 4。 Is this possible to do?这是可能的吗? I tried a few ways but nothing is working for me, any help would be greatly appreciated!我尝试了几种方法,但没有任何方法对我有用,任何帮助将不胜感激!
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()
}
}
}
尝试:
orderNumberLabel.text = String(items.count)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.