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)
}
}
Post
Replies
Boosts
Views
Activity
Anyone? or, maybe I post the question at wrong place?
Does anyone have any suggestions?
I just got it working again after remove the FBAudience framework.
I have the same problem. That does not make sense.. if you remove FBAudience framework then FBAudience will not wokr? True?
Duplicated
I am running into rather interesting situation:
If I put in limited information to generate QRCode, it works perfect on reading QRCode. Information like N,ORG,TITLE,ADR (without country;United States of America)
As soon as I add more information like "United States of America", or email address, the QRCode generated will no longer readable.
I checked limitation of QRCode, somewhere mentioned RACode limited to 4,269 alphanumeric characters, not sure that's the reason?!
..
Yes, CIQRCodeGenerator, which one I should use for more characters?
Tried.. No luck. It seems it won't even go beyond 500 characters. Here's my basic code:
textToQRCode = "BEGIN:VCARD\n" +
"N:\(lastName);\(firstName)\n" +
"TITLE:Admin \n" +
"TEL:\(phoneNumber)\n" +
"Email:\(email)\n" +
"ADR;WORK:;;\(street);\(city);\(state);\(zipCode);\(country) \n" + //working
"NOTE:\(notes)\n" +
"END:VCARD"
if let qrImage = generateQRCode(textToQRCode) {
imageBox.image = qrImage
}
//
func generateQRCode(_ string: String) -> UIImage? {
let data = string.data(using: String.Encoding.utf8) //for foreign characters
if let QRFilter = CIFilter(name: "CIQRCodeGenerator") {
QRFilter.setValue(data, forKey: "inputMessage")
guard let QRImage = QRFilter.outputImage else {return nil}
//To scale up the code without losing resolution
let transformScale = CGAffineTransform(scaleX: 10, y: 10)
let scaledQRImage = QRImage.transformed(by: transformScale)
//processing to get the UIImage
let context = CIContext()
let cgImage = context.createCGImage(scaledQRImage, from: scaledQRImage.extent)
let processedImage = UIImage(cgImage: cgImage!)
//return processed image
return processedImage
}
return nil
}
QRCode always generated (no character problems), but not readable by camera (or, other 3rd party reader), It will read when total characters count is small.
This is the one generated and will work, add anymore character will not work..
I believe I've found the problem, its the printed code (image on top), not clear and fuzzy. Print is another matter. I will work on that. If problem I will start another request. Thanks.
This morning I got excited, finally the Xcode 14.3.1 released and I quickly upgrade my system. SAD, SAD.. now it will not even load any project, it simply lock the system. I download the Xcode from developer site, something.. Lock the system. Now, I can't even do any work...
I am 75 and I have learned iOS for the last 2 years, now I have 6 apps on Apple App Store: DCAvg, QRCode++, Currency++, TextScanner++ and bNotes.
It keeps me young and good for my health.