英文:
how can I correctly get the tableCell value to next tableCell
问题
I understand that you are facing an issue with displaying cells in the next view controller, and you want to know how to resolve it. Unfortunately, without access to the simulator screenshots, I cannot provide specific guidance on the issue you are encountering.
However, based on your code, it seems that you are passing cell values from the InvoiceViewController
to the InvoiceDetailViewController
. Here are a few general troubleshooting steps you can follow to address issues with cell display in the next view controller:
-
Verify Data Passing:
- Ensure that you are correctly passing the cell values from
InvoiceViewController
toInvoiceDetailViewController
. Check the data being passed and make sure it matches your expectations.
- Ensure that you are correctly passing the cell values from
-
Cell Configuration:
- Double-check the configuration of your cell labels in
InvoiceDetailViewController
. Ensure that the frame positions and data assignments are correct.
- Double-check the configuration of your cell labels in
-
Table View Reloading:
- Make sure to call
tableViewww.reloadData()
after setting thecellValues
array inInvoiceDetailViewController
. This ensures that the table view updates with the new data.
- Make sure to call
-
Debugging:
- Use debugging tools (breakpoints, print statements) to inspect the values of variables and verify that they are as expected.
-
Check for Errors:
- Look for any error messages or warnings in the Xcode console that might provide insights into the issue.
If you encounter a specific error or have further details about the problem you are facing, please provide those, and I can offer more targeted assistance.
英文:
I'm new here and new to swift .now I'm creating an invoice app so I need to display the invoiceViewController in next ViewController but the cells for the bills in invoice are not coming correctly to next page like being an rangeProblem. I have tried severMethod but I'm getting disappointed . could any one hear help me!..
this is my InvoiceViewController
enum selecteddScope:Int {
case Name = 0
case Code = 1
case Price = 2
case Quantity = 3
case Total = 4
}
import UIKit
import CoreData
class InvoiceViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
@IBOutlet var searchBar: UISearchBar!
@IBOutlet var invoiceNo: UITextField?
@IBOutlet var customerName: UITextField?
@IBOutlet var invoiceDate: UIDatePicker?
@IBOutlet var tableView: UITableView!
@IBOutlet var totalAmountLabel: UILabel!
@IBOutlet var discountLabel: UILabel!
@IBOutlet var netAmountLabel: UILabel!
var selectedCellValues: [String] = []
var invoices: [NSManagedObject] = []
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var currentInvoice: Invoice?
let appDelegate = UIApplication.shared.delegate as! AppDelegate
var filteringInvoices: [NSManagedObject] = []
var isSearching = false
var managedObjectContext: NSManagedObjectContext!
var totalSum = 0.0
override func viewDidLoad() {
super.viewDidLoad()
loadDataFromDatabases()
tableView.reloadData()
searchBar.delegate = self
saveToOurCoreData()
searchBarSetuP()
fetchInvoices()
tableView.reloadData()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "celll")
self.navigationItem.rightBarButtonItem = self.editButtonItem
}
func searchBarSetuP() {
let searchBar = UISearchBar(frame: CGRect(x:0,y:0,width:(UIScreen.main.bounds.width),height:70))
searchBar.showsScopeBar = true
searchBar.scopeButtonTitles = ["Name","Code","Price","Quantity","Total"]
searchBar.selectedScopeButtonIndex = 0
searchBar.delegate = self
self.tableView.tableHeaderView = searchBar
}
func fetchInvoices() {
do {
invoices = try context.fetch(Invoice.fetchRequest())
} catch {
print("Error fetching invoice: \(error.localizedDescription)")
}
tableView.reloadData()
}
func filteringInvoices(with searchText: String) {
let predicate = NSPredicate(format: "productName CONTAINS[c] %@ OR productCode CONTAINS[c] %@ OR unitPrice == %@ OR quantity == %@ OR totalAmount == %@", searchText, searchText, searchText, searchText, searchText)
filteringInvoices = invoices.filter { predicate.evaluate(with: $0) }
tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchText.isEmpty {
isSearching = true
filteringInvoices = invoices
} else {
isSearching = false
filteringInvoices(with: searchText)
// filterContentForSearchText(searchText, scope: searchBar.selectedScopeButtonIndex)
}
tableView.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
loadDataFromDatabases()
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loadDataFromDatabases() {
//Set up Core Data Context
let context = appDelegate.persistentContainer.viewContext
//Set up Request
let request = NSFetchRequest<NSManagedObject>(entityName: "Invoice")
//Execute request
do {
invoices = try context.fetch(request)
} catch _ as NSError {
// print("Could not fetch. \(error), \(error.userInfo)")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return isSearching ? filteringInvoices.count : invoices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "celll", for: indexPath)
let invoice: Invoice
if isSearching {
invoice = filteringInvoices[indexPath.row] as! Invoice
} else {
invoice = invoices[indexPath.row] as! Invoice
}
// Create labels for each column
let productNameLabel = UILabel()
let productCodeLabel = UILabel()
let unitPriceLabel = UILabel()
let quantityLabel = UILabel()
let totalLabel = UILabel()
// Configure label properties
productNameLabel.text = invoice.productName
productNameLabel.textAlignment = .left
productNameLabel.frame = CGRect(x: 15, y: 10, width: 100, height: 30)
productCodeLabel.text = invoice.productCode
productCodeLabel.textAlignment = .left
productCodeLabel.frame = CGRect(x: 100, y: 10, width: 100, height: 30)
unitPriceLabel.text = String(invoice.unitPrice)
unitPriceLabel.textAlignment = .left
unitPriceLabel.frame = CGRect(x: 180, y: 10, width: 100, height: 30)
quantityLabel.text = String(invoice.quantity)
quantityLabel.textAlignment = .left
quantityLabel.frame = CGRect(x: 250, y: 10, width: 100, height: 30)
totalLabel.text = String(invoice.totalAmount)
totalLabel.textAlignment = .right
totalLabel.frame = CGRect(x: 270, y: 10, width: 100, height: 30)
// Remove existing labels from cell's contentView
cell.contentView.subviews.forEach { $0.removeFromSuperview()
}
// Add labels to the cell's contentView
cell.contentView.addSubview(productNameLabel)
cell.contentView.addSubview(productCodeLabel)
cell.contentView.addSubview(unitPriceLabel)
cell.contentView.addSubview(quantityLabel)
cell.contentView.addSubview(totalLabel)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Get the selected item
if let selecteddInvoice = invoices[indexPath.row] as? Invoice {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "ProductDetailViewController") as? ProductDetailViewController {
controller.selecteddItem = selecteddInvoice
self.navigationController?.pushViewController(controller, animated: true)
} else {
print("Failed to instantiate DetailViewController")
}
}
}
@IBAction func onClickSaveInvoice(_ sender: UIButton) {
//saveToOurCoreData()
guard UIApplication.shared.delegate is AppDelegate else {
return
}
// Save the calculated values
let totalSum = calculateTotalAmount()
let netAmount = totalSum - 100
do {
// Pass the calculated values to InvoiceDetailViewController
if let selectedInvoice = invoices.first as? Invoice {
let section = 0
let rowIndexes = 0...5
var selectedCellValues: [String] = []
for row in rowIndexes {
let indexPath = IndexPath(row: row, section: section)
let cell = tableView.dequeueReusableCell(withIdentifier: "celll", for: indexPath)
}
if let selectedIndexPath = tableView.indexPathForSelectedRow {
if let selectedCell = tableView.cellForRow(at: selectedIndexPath) {
for subview in selectedCell.contentView.subviews {
if let label = subview as? UILabel {
if let labelText = label.text {
selectedCellValues.append(labelText)
print("label.text")
}
}
else if let otherLabel1 = subview as? UILabel {
if let otherLabel1Text = otherLabel1.text {
selectedCellValues.append(otherLabel1Text)
print("otherLabel1.text")
}
}
else if let otherLabel2 = subview as? UILabel {
if let otherLabel2Text = otherLabel2.text {
selectedCellValues.append(otherLabel2Text)
}
}
else if let otherLabel3 = subview as? UILabel {
if let otherLabel3Text = otherLabel3.text {
selectedCellValues.append(otherLabel3Text)
}
print(selectedCellValues)
}
}
}
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "InvoiceDetailViewController") as? InvoiceDetailViewController {
controller.selectedddItem = selectedInvoice
controller.cellValues = selectedCellValues
controller.invoice = selectedInvoice
controller.invoice?.customer = customerName?.text
controller.invoice?.number = invoiceNo?.text
controller.invoice?.amountTotal = String(totalSum)
controller.invoice?.netTotal = String(netAmount)
controller.invoice?.date = invoiceDate?.date
self.navigationController?.pushViewController(controller, animated: true)
} else {
print("Failed to instantiate InvoiceDetailViewController")
}
}
} catch _ as NSError {
// print("Could not save. \(error), \(error.userInfo)")
}
}
func saveToOurCoreData() {
guard UIApplication.shared.delegate is AppDelegate else {
return
}
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSManagedObject>(entityName: "Invoice")
do {
invoices = try context.fetch(request)
// Calculate total amount
var totalSum = 0.0
for invoice in invoices {
if let amount = invoice.value(forKey: "totalAmount") as? Double {
totalSum += amount
}
}
totalAmountLabel.text = String(totalSum)
// Calculate net amount
let netAmount = totalSum - 100
netAmountLabel.text = String(netAmount)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
func calculateTotalAmount() -> Double {
var totalSum = 0.0
for invoice in invoices {
if let amount = invoice.value(forKey: "totalAmount") as? Double {
totalSum += amount
}
}
return totalSum
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
let invoice = invoices[indexPath.row] as? Invoice
let name = invoice!.productName
let alertController = UIAlertController(title: "Selected row: \(indexPath.row) (\(name))", message: "Are you sure you want to delete this Product?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "No", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
let deleteAction = UIAlertAction(title: "Yes", style: .destructive) { [self] _ in
let context = appDelegate.persistentContainer.viewContext
context.delete(invoice!)
do {
try context.save()
loadDataFromDatabases()
tableView.deleteRows(at: [indexPath], with: .fade)
}
catch {
fatalError("Error saving context: \(error)")
}
}
// loadDataFromDatabase()
alertController.addAction(deleteAction)
present(alertController, animated: true, completion: nil)
// tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "invoiceDetailViewController" {
let destinationVC1 = segue.destination as! InvoiceDetailViewController
}
}
}
extension InvoiceViewController: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
filterContentForSearchText(searchBar.text ?? "", scope: selectedScope)
}
func filterContentForSearchText(_ searchText: String, scope: Int) {
var predicate: NSPredicate?
switch scope {
case 0: // Name
predicate = NSPredicate(format: "productName CONTAINS[c] %@", searchText)
case 1: // Code
predicate = NSPredicate(format: "productCode CONTAINS[c] %@", searchText)
case 2: // Price
predicate = NSPredicate(format: "unitPrice == %@", searchText)
case 3: // Quantity
predicate = NSPredicate(format: "quantity == %@", searchText)
case 4: // Amount
predicate = NSPredicate(format: "totalAmount == %@", searchText)
default:
predicate = nil
}
if let predicate = predicate {
filteringInvoices = invoices.filter { predicate.evaluate(with: $0) }
} else {
filteringInvoices = invoices
}
tableView.reloadData()
}
}
and this is my detailInvoiceViewController
class InvoiceDetailViewController: UIViewController, UITableViewDataSource {
@IBOutlet var labelOfDate: UILabel!
@IBOutlet var labelOfCustomer: UILabel!
@IBOutlet var labelOfNumber: UILabel!
@IBOutlet var labelOfTotalAmount: UILabel!
@IBOutlet var labelOfNetAmount: UILabel!
@IBOutlet var tableViewww: UITableView!
var cellValues: [String] = []
var selectedddItem: Invoice?
var invoice: Invoice?
var invoices: [Invoice] = []
override func viewDidLoad() {
super.viewDidLoad()
// tableViewww.reloadData()
if let invoice = selectedddItem {
labelOfCustomer.text = invoice.customer
labelOfNumber.text = invoice.number
labelOfTotalAmount.text = invoice.amountTotal
labelOfNetAmount.text = invoice.netTotal
// Convert Date to String using a DateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
if let date = invoice.date {
labelOfDate.text = dateFormatter.string(from: date)
} else {
labelOfDate.text = ""
}
// Update cell values
cellValues = [
invoice.productName ?? "",
invoice.productCode ?? "",
String(invoice.unitPrice),
String(invoice.quantity),
String(invoice.totalAmount),
]
tableViewww.reloadData()
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows based on your data
return cellValues.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cells", for: indexPath)
let productNameLabel1 = UILabel()
let productCodeLabel2 = UILabel()
let unitPriceLabel3 = UILabel()
let quantityLabel4 = UILabel()
let totalLabel5 = UILabel()
productNameLabel1.text = cellValues[indexPath.row]
productNameLabel1.textAlignment = .left
productNameLabel1.frame = CGRect(x: 15, y: 10, width: 100, height: 30)
productCodeLabel2.text = cellValues[1]
productCodeLabel2.textAlignment = .left
productCodeLabel2.frame = CGRect(x: 100, y: 10, width: 100, height: 30)
unitPriceLabel3.text = cellValues[2]
unitPriceLabel3.textAlignment = .left
unitPriceLabel3.frame = CGRect(x: 180, y: 10, width: 100, height: 30)
quantityLabel4.text = cellValues[3]
quantityLabel4.textAlignment = .left
quantityLabel4.frame = CGRect(x: 250, y: 10, width: 100, height: 30)
totalLabel5.text = cellValues[4]
totalLabel5.textAlignment = .right
totalLabel5.frame = CGRect(x: 270, y: 10, width: 100, height: 30)
cell.contentView.subviews.forEach { $0.removeFromSuperview() }
cell.contentView.addSubview(productNameLabel1)
cell.contentView.addSubview(productCodeLabel2)
cell.contentView.addSubview(unitPriceLabel3)
cell.contentView.addSubview(quantityLabel4)
cell.contentView.addSubview(totalLabel5)
//tableViewww.reloadData()
return cell
}
}
I have tried for this cells displaying in next ViewController cell but it showing in incorrect manner of being incorrect range.
I will share my simulator screenshots so you guys can point out what I'm saying and my problem.
答案1
得分: 1
这是详细的控制器。
class InvoiceDetailViewController: UIViewController, UITableViewDataSource {
@IBOutlet var labelOfDate: UILabel!
@IBOutlet var labelOfCustomer: UILabel!
@IBOutlet var labelOfNumber: UILabel!
@IBOutlet var labelOfTotalAmount: UILabel!
@IBOutlet var labelOfNetAmount: UILabel!
@IBOutlet var tableViewww: UITableView!
var cellValues: [String] = []
var selectedddItem: Invoice?
var invoice: Invoice?
var invoices: [Invoice] = [] {
didSet {tableView.reloadData()}
}
override func viewDidLoad() {
super.viewDidLoad()
// tableViewww.reloadData()
if let invoice = selectedddItem {
labelOfCustomer.text = invoice.customer
labelOfNumber.text = invoice.number
labelOfTotalAmount.text = invoice.amountTotal
labelOfNetAmount.text = invoice.netTotal
// 使用DateFormatter将日期转换为字符串
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
if let date = invoice.date {
labelOfDate.text = dateFormatter.string(from: date)
} else {
labelOfDate.text = ""
}
// 更新单元格值
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return invoices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "celll", for: indexPath)
let invoice: Invoice
if isSearching {
invoice = filteringInvoices[indexPath.row] as! Invoice
} else {
invoice = invoices[indexPath.row] as! Invoice
}
// 为每一列创建标签
let productNameLabel = UILabel()
let productCodeLabel = UILabel()
let unitPriceLabel = UILabel()
let quantityLabel = UILabel()
let totalLabel = UILabel()
// 配置标签属性
productNameLabel.text = invoice.productName
productNameLabel.textAlignment = .left
productNameLabel.frame = CGRect(x: 15, y: 10, width: 100, height: 30)
productCodeLabel.text = invoice.productCode
productCodeLabel.textAlignment = .left
productCodeLabel.frame = CGRect(x: 100, y: 10, width: 100, height: 30)
unitPriceLabel.text = String(invoice.unitPrice)
unitPriceLabel.textAlignment = .left
unitPriceLabel.frame = CGRect(x: 180, y: 10, width: 100, height: 30)
quantityLabel.text = String(invoice.quantity)
quantityLabel.textAlignment = .left
quantityLabel.frame = CGRect(x: 250, y: 10, width: 100, height: 30)
totalLabel.text = String(invoice.totalAmount)
totalLabel.textAlignment = .right
totalLabel.frame = CGRect(x: 270, y: 10, width: 100, height: 30)
// 从单元格的contentView中删除现有的标签
cell.contentView.subviews.forEach { $0.removeFromSuperview() }
// 将标签添加到单元格的contentView
cell.contentView.addSubview(productNameLabel)
cell.contentView.addSubview(productCodeLabel)
cell.contentView.addSubview(unitPriceLabel)
cell.contentView.addSubview(quantityLabel)
cell.contentView.addSubview(totalLabel)
return cell
}
}
按钮操作
@IBAction func onClickSaveInvoice(_ sender: UIButton) {
//saveToOurCoreData()
guard UIApplication.shared.delegate is AppDelegate else {
return
}
// 保存计算出的值
let totalSum = calculateTotalAmount()
let netAmount = totalSum - 100
do {
// 将计算出的值传递给InvoiceDetailViewController
if let selectedInvoice = invoices.first as? Invoice {
let section = 0
let rowIndexes = 0...5
var selectedCellValues: [String] = []
for row in rowIndexes {
let indexPath = IndexPath(row: row, section: section)
let cell = tableView.dequeueReusableCell(withIdentifier: "celll", for: indexPath)
}
if let selectedIndexPath = tableView.indexPathForSelectedRow {
if let selectedCell = tableView.cellForRow(at: selectedIndexPath) {
for subview in selectedCell.contentView.subviews {
if let label = subview as? UILabel {
if let labelText = label.text {
selectedCellValues.append(labelText)
print("label.text")
}
}
else if let otherLabel1 = subview as? UILabel {
if let otherLabel1Text = otherLabel1.text {
selectedCellValues.append(otherLabel1Text)
print("otherLabel1.text")
}
}
else if let otherLabel2 = subview as? UILabel {
if let otherLabel2Text = otherLabel2.text {
selectedCellValues.append(otherLabel2Text)
}
}
else if let otherLabel3 = subview as? UILabel {
if let otherLabel3Text = otherLabel3.text {
selectedCellValues.append(otherLabel3Text)
}
print(selectedCellValues)
}
}
}
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "InvoiceDetailViewController") as? InvoiceDetailViewController {
controller.selectedddItem = selectedInvoice
controller.invoices = invoices as? [Invoice] ?? []
controller.invoice = selectedInvoice
controller.invoice?.customer = customerName?.text
controller.invoice?.number = invoiceNo?.text
controller.invoice?.amountTotal = String(totalSum)
controller.invoice?.netTotal = String(netAmount)
controller.invoice?.date = invoiceDate?.date
self.navigationController?.pushViewController(controller, animated: true)
} else {
print("Failed to instantiate InvoiceDetailViewController")
}
}
} catch _ as NSError {
// print("Could not save. \(error), \(error.userInfo)")
}
}
英文:
This the detail controller.
class InvoiceDetailViewController: UIViewController, UITableViewDataSource {
@IBOutlet var labelOfDate: UILabel!
@IBOutlet var labelOfCustomer: UILabel!
@IBOutlet var labelOfNumber: UILabel!
@IBOutlet var labelOfTotalAmount: UILabel!
@IBOutlet var labelOfNetAmount: UILabel!
@IBOutlet var tableViewww: UITableView!
var cellValues: [String] = []
var selectedddItem: Invoice?
var invoice: Invoice?
var invoices: [Invoice] = [] {
didSet {tableView.reloadData()}
}
override func viewDidLoad() {
super.viewDidLoad()
// tableViewww.reloadData()
if let invoice = selectedddItem {
labelOfCustomer.text = invoice.customer
labelOfNumber.text = invoice.number
labelOfTotalAmount.text = invoice.amountTotal
labelOfNetAmount.text = invoice.netTotal
// Convert Date to String using a DateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yyyy"
if let date = invoice.date {
labelOfDate.text = dateFormatter.string(from: date)
} else {
labelOfDate.text = ""
}
// Update cell values
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return invoices.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "celll", for: indexPath)
let invoice: Invoice
if isSearching {
invoice = filteringInvoices[indexPath.row] as! Invoice
} else {
invoice = invoices[indexPath.row] as! Invoice
}
// Create labels for each column
let productNameLabel = UILabel()
let productCodeLabel = UILabel()
let unitPriceLabel = UILabel()
let quantityLabel = UILabel()
let totalLabel = UILabel()
// Configure label properties
productNameLabel.text = invoice.productName
productNameLabel.textAlignment = .left
productNameLabel.frame = CGRect(x: 15, y: 10, width: 100, height: 30)
productCodeLabel.text = invoice.productCode
productCodeLabel.textAlignment = .left
productCodeLabel.frame = CGRect(x: 100, y: 10, width: 100, height: 30)
unitPriceLabel.text = String(invoice.unitPrice)
unitPriceLabel.textAlignment = .left
unitPriceLabel.frame = CGRect(x: 180, y: 10, width: 100, height: 30)
quantityLabel.text = String(invoice.quantity)
quantityLabel.textAlignment = .left
quantityLabel.frame = CGRect(x: 250, y: 10, width: 100, height: 30)
totalLabel.text = String(invoice.totalAmount)
totalLabel.textAlignment = .right
totalLabel.frame = CGRect(x: 270, y: 10, width: 100, height: 30)
// Remove existing labels from cell's contentView
cell.contentView.subviews.forEach { $0.removeFromSuperview()
}
// Add labels to the cell's contentView
cell.contentView.addSubview(productNameLabel)
cell.contentView.addSubview(productCodeLabel)
cell.contentView.addSubview(unitPriceLabel)
cell.contentView.addSubview(quantityLabel)
cell.contentView.addSubview(totalLabel)
return cell
}
}
the button action
@IBAction func onClickSaveInvoice(_ sender: UIButton) {
//saveToOurCoreData()
guard UIApplication.shared.delegate is AppDelegate else {
return
}
// Save the calculated values
let totalSum = calculateTotalAmount()
let netAmount = totalSum - 100
do {
// Pass the calculated values to InvoiceDetailViewController
if let selectedInvoice = invoices.first as? Invoice {
let section = 0
let rowIndexes = 0...5
var selectedCellValues: [String] = []
for row in rowIndexes {
let indexPath = IndexPath(row: row, section: section)
let cell = tableView.dequeueReusableCell(withIdentifier: "celll", for: indexPath)
}
if let selectedIndexPath = tableView.indexPathForSelectedRow {
if let selectedCell = tableView.cellForRow(at: selectedIndexPath) {
for subview in selectedCell.contentView.subviews {
if let label = subview as? UILabel {
if let labelText = label.text {
selectedCellValues.append(labelText)
print("label.text")
}
}
else if let otherLabel1 = subview as? UILabel {
if let otherLabel1Text = otherLabel1.text {
selectedCellValues.append(otherLabel1Text)
print("otherLabel1.text")
}
}
else if let otherLabel2 = subview as? UILabel {
if let otherLabel2Text = otherLabel2.text {
selectedCellValues.append(otherLabel2Text)
}
}
else if let otherLabel3 = subview as? UILabel {
if let otherLabel3Text = otherLabel3.text {
selectedCellValues.append(otherLabel3Text)
}
print(selectedCellValues)
}
}
}
}
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "InvoiceDetailViewController") as? InvoiceDetailViewController {
controller.selectedddItem = selectedInvoice
controller.invoices = invoices as? [Invoice] ?? []
controller.invoice = selectedInvoice
controller.invoice?.customer = customerName?.text
controller.invoice?.number = invoiceNo?.text
controller.invoice?.amountTotal = String(totalSum)
controller.invoice?.netTotal = String(netAmount)
controller.invoice?.date = invoiceDate?.date
self.navigationController?.pushViewController(controller, animated: true)
} else {
print("Failed to instantiate InvoiceDetailViewController")
}
}
} catch _ as NSError {
// print("Could not save. \(error), \(error.userInfo)")
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论