gpt4 book ai didi

ios - Swift - 来自结构数组的数据未显示在 TableView 中

转载 作者:行者123 更新时间:2023-11-28 15:00:39 26 4
gpt4 key购买 nike

如何在 TableView 上显示数据,其起源于结构和一个用于添加操作的额外单元格?

所有数据(发票和项目)都是从服务器获取的,并且工作正常。

如果我添加打印,它会在控制台上显示所有数据。

[Invoice.InvoiceItem(invoice_detail_id: 13, description: oi, unit_price: 2, quantity: 1), Invoice.InvoiceItem(invoice_detail_id: 14, description: re, unit_price: 3, quantity: 2), Invoice.InvoiceItem(invoice_detail_id: 15, description: fsdf, unit_price: 4, quantity: 3)]

问题是它没有填充到 tableview 上。我尝试加或减 1,但没有成功。

另外,还有一个cell,不是items,而是添加新item的cell,当没有item的时候总是最后一个cell或者唯一一个。

tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 似乎只被调用一次...实际上它应该是 4(3 个项目加上添加单元格)

import UIKit

class InvoiceItemCell: UITableViewCell {
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var unitPriceQuantityLabel: UILabel!
@IBOutlet weak var lineTotalLabel: UILabel!

}

class InvoiceViewController: UIViewController, AccessoryToolbarDelegate,UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var dateIssueTextField: UITextField!
@IBOutlet weak var dueDateTextField: UITextField!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var taxDescriptionLabel: UILabel!

var thePicker = UIDatePicker()
var invoice: Invoice?
var client: Client?
var invoiceItems : [InvoiceItem] = [InvoiceItem]()

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
dateIssueTextField.delegate = self
dueDateTextField.delegate = self

self.thePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
self.thePicker.backgroundColor = UIColor.white
self.thePicker.datePickerMode = UIDatePickerMode.date

//data will be loaded from an API to a server. Hard Coded now just for testing
setUpTextFieldPicker(textField: dateIssueTextField)
setUpTextFieldPicker(textField: dueDateTextField)

tableView.rowHeight = UITableViewAutomaticDimension
tableView.rowHeight = 75

}

override func viewWillAppear(_ animated: Bool) {
self.title = "New"
taxDescriptionLabel.text = "Tax @ 0%"

if (invoice?.invoice_id) != nil {
self.title = "Edit"
dueDateTextField.text = invoice?.due_date
dateIssueTextField.text = invoice?.date_issue
let taxDescription = invoice?.tax.description
taxDescriptionLabel.text = "Tax @ \(taxDescription ?? "0")%"

let invoiceItems = invoice?.items


print("Will Appear")
print(invoiceItems!)
}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return invoiceItems.count + 1
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if ((indexPath as NSIndexPath).row == (invoiceItems.count)) {
return 50
} else {
return 75
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

//if ((indexPath as NSIndexPath).row == (invoiceItems.count)) {
print(indexPath.row)
if (indexPath.row == (invoiceItems.count)) {
let cell = tableView.dequeueReusableCell(withIdentifier: "InvoiceAddCell", for: indexPath)
print("add cell")
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "InvoiceItemCell", for: indexPath) as! InvoiceItemCell
let item = invoiceItems[indexPath.row]
print("Cell")

cell.descriptionLabel.text = item.description

let partial = (item.unit_price?.description)!+" X "+(item.quantity?.description)!
cell.unitPriceQuantityLabel.text = partial

let lineTotal = item.unit_price * item.quantity
cell.lineTotalLabel.text = lineTotal.description
return cell
}



}


func doneClicked(for textField: UITextField) {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .none

//show on text field
dateFormatter.dateFormat = "dd MMM yyyy"
textField.text = dateFormatter.string(from: thePicker.date)

//formated to store on mysql
dateFormatter.dateFormat = "yyyy-MM-dd"
let finalDate: String = dateFormatter.string(from: thePicker.date)
print(finalDate)
textField.resignFirstResponder()
}

func cancelClicked(for textField: UITextField) {
textField.resignFirstResponder()
}

func setUpTextFieldPicker(textField: UITextField) {
textField.inputView = thePicker
let toolbar = AccessoryToolbar(for: textField)
toolbar.accessoryDelegate = self
}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy"
if let fullDate = dateFormatter.date(from: textField.text!) {
thePicker.date = fullDate
} else {
thePicker.date = Date()
}

return true
}



}

结构

struct InvoiceItem: Codable {
var invoice_detail_id: Int!
let description: String!
let unit_price: Decimal!
let quantity: Decimal!

init(invoice_detail_id: Int! = nil, description: String, unit_price: Decimal, quantity: Decimal) {
self.invoice_detail_id = invoice_detail_id
self.description = description
self.unit_price = unit_price
self.quantity = quantity
}

}

//let invoiceItems = [InvoiceItem(invoice_detail_id: 1, description: "AB", unit_price: 10.22, quantity: 2),
// InvoiceItem(invoice_detail_id: 2, description: "fsdsdsdfsdf", unit_price: 44.35, quantity: 10)]

struct Invoice: Codable {
var invoice_id: Int!
let client_id: Int!
let tax: Decimal!
let date_issue: String!
let due_date: String!
let amount_paid: Decimal!
let date_transaction: String!
let voided: Int!
let note: String!
let invoice_number: String!
let status: String!
let items: [InvoiceItem]!

init(invoice_id: Int! = nil, client_id: Int! = nil,tax: Decimal, date_issue: String, due_date: String, amount_paid: Decimal, date_transaction: String, voided: Int, note: String, invoice_number: String, status: String, items: InvoiceItem) {
self.invoice_id = invoice_id
self.client_id = client_id
self.tax = tax
self.date_issue = date_issue
self.due_date = due_date
self.amount_paid = amount_paid
self.date_transaction = date_transaction
self.voided = voided
self.note = note
self.invoice_number = invoice_number
self.status = status
self.items = [items]

}
}

最佳答案

问题出在 viewWillAppear 上。本地 invoiceItems 没有 TableView 数据源使用的 invoiceItems 属性。

修改如下:

override func viewWillAppear(_ animated: Bool) {
self.title = "New"
taxDescriptionLabel.text = "Tax @ 0%"

if (invoice?.invoice_id) != nil {
self.title = "Edit"
dueDateTextField.text = invoice?.due_date
dateIssueTextField.text = invoice?.date_issue
let taxDescription = invoice?.tax.description
taxDescriptionLabel.text = "Tax @ \(taxDescription ?? "0")%"

invoiceItems = invoice?.items // No let

print("Will Appear")
print(invoiceItems!)
}
}

所有这些代码确实应该在 viewDidLoad 中,而不是 viewWillAppear

关于ios - Swift - 来自结构数组的数据未显示在 TableView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48966077/

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