Claude31: I am still confused. Since my code is short, I will post is below for you to help pointing out the mistakes..
Code crashes when remove element from array and delete from table.. Thank You!
//data model
import Foundation
//Trade data model
struct Trade {
var id: Int
var date: Date
var symbol: String
var qty: Double
var price: Double
}
//Group Trade by symbol for tableView Section
struct sectionBySymbol {
var symbol: String
var trades: [Trade]
}
//code
import UIKit
class SectionTableViewController: UITableViewController {
//for Sections
var sections = [sectionBySymbol]()
var trades: [Trade] = [
Trade(id: 1, date: stringToDate("04/18/2022 10:05"), symbol: "TSLA", qty: 10, price: 989.20),
Trade(id: 2, date: stringToDate("04/19/2022 11:30"), symbol: "TSLA", qty: 20, price: 970),
Trade(id: 3, date: stringToDate("04/20/2022 08:13"), symbol: "NVDA", qty: 50, price: 220.25),
Trade(id: 4, date: stringToDate("04/20/2022 09:23"), symbol: "AFRM", qty: 25, price: 40.25),
Trade(id: 5, date: stringToDate("04/19/2022 07:20"), symbol: "AFRM", qty: 30, price: 32.95),
Trade(id: 6, date: stringToDate("04/17/2022 11:21"), symbol: "IBM", qty: 25, price: 110.25),
Trade(id: 7, date: stringToDate("04/20/2022 09:23"), symbol: "IBM", qty: 25, price: 112.00),
Trade(id: 8, date: stringToDate("04/19/2022 07:20"), symbol: "NFLX", qty: 40, price: 222.15),
Trade(id: 9, date: stringToDate("04/17/2022 11:21"), symbol: "NVDA", qty: 100, price: 199.25),
Trade(id: 10, date: stringToDate("04/20/2022 09:23"), symbol: "TSLA", qty: 25, price: 1110.25),
Trade(id: 11, date: stringToDate("04/19/2022 07:20"), symbol: "AFRM", qty: 30, price: 34.00),
Trade(id: 12, date: stringToDate("04/17/2022 11:21"), symbol: "IBM", qty: 25, price: 99.80)
]
override func viewDidLoad() {
super.viewDidLoad()
//set title
navigationItem.title = "Cost Average"
navigationItem.leftBarButtonItem = editButtonItem
let groupBySymbol = Dictionary(grouping: trades, by: \.symbol)
self.sections = groupBySymbol.map {(Key, Value) in
return sectionBySymbol(symbol: Key, trades: Value)
}
}
// MARK: - Table view data source
//numberOfSections
override func numberOfSections(in tableView: UITableView) -> Int {
// return number of sections
return self.sections.count //number of section
}
//numberOfRowsInSection
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].trades.count
}
//cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
//decimal number with comma
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
var content = cell.defaultContentConfiguration()
let section = self.sections[indexPath.section]
let trade = section.trades[indexPath.row]
content.text = "\(trade.symbol) \(String(format: "%.0f", trade.qty)) x $\(String(format: "%.2f", trade.price)) = \(formatter.string(from: NSNumber(value: trade.qty * trade.price)) ?? "0.00")"
content.secondaryText = "\(dateToString(trade.date))"
cell.contentConfiguration = content
return cell
}
// viewForHeaderInSection to create Section Header
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let titleLabel = UILabel()
titleLabel.text = "Test"
titleLabel.backgroundColor = UIColor.gray
return (titleLabel)
}
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
trades.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
let movedTrade = trades.remove(at: fromIndexPath.row)
trades.insert(movedTrade, at: to.row)
}
}
Topic:
Programming Languages
SubTopic:
Swift
Tags: