I cant get my calculations print in the label - ios

I am trying to get the values to calculate the profit and print out the calculated numbers in a label, but it isn't printing it out in the label. When I hover over "outputValue" after the app crashed it has the calculated value stored in it, but for some reason it doesn't print out in the label and gives me the error
Cannot assign value of type 'Double' to type 'String?'
at line ProfitLabel.text = (outputValue) in insertNewItems(). Is there any way to fix this?
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 ProfitLabel: RoundedLabel2!
#IBOutlet weak var TotalLabel: RoundedLabel2!
#IBOutlet weak var itemTextField: UITextField!
#IBOutlet weak var priceTextField: UITextField!
#IBOutlet weak var saleTextField: UITextField!
var items: [ItemRow] = []
struct ItemRow: Codable
{
var first: String
var second: String
var third: String
}
override func viewDidLoad() {
super.viewDidLoad()
load()
// tableView.tableFooterView = UIView(frame: CGRect.zero)
}
func save(){
if let data = try? PropertyListEncoder().encode(items) {
UserDefaults.standard.set(data, forKey: "SavedItems")
}
}
func load(){
let defaults = UserDefaults.standard
if let data = defaults.data(forKey: "SavedItems") {
items = try! PropertyListDecoder().decode([ItemRow].self, from: data)
}
}
#IBAction func addButtonTapped(_ sender: Any) {
insertNewItems()
save()
}
func insertNewItems() {
let newVideoRow = ItemRow(first: itemTextField.text!, second: priceTextField.text!, third: saleTextField.text!)
items.append(newVideoRow)
let indexPath = IndexPath(row: items.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
let priceValue = Double(priceTextField.text!)
let saleValue = Double(saleTextField.text!)
if priceValue != nil && saleValue != nil {
let outputValue = Double (saleValue! - priceValue!)
ProfitLabel.text = (outputValue)
}
itemTextField.text = ""
priceTextField.text = ""
saleTextField.text = ""
view.endEditing(true)
}
}
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.first
cell.priceLabel.text = ItemTitle.second
cell.saleLabel.text = ItemTitle.third
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.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
save()
}
}
}

Simply disconnect you ProfitLabel and reconnect the outlet.
Also try convert output value to string like this:
ProfitLabel.text = "\(output)"

The error is clear: You are going to assign a Double to a text property which is impossible.
There is an easy solution
ProfitLabel.text = String(outputValue)
However your code is cumbersome and objective-c-ish, the recommended Swift syntax is
if let priceValue = Double(priceTextField.text!),
let saleValue = Double(saleTextField.text!) {
ProfitLabel.text = String(saleValue - priceValue)
}
If the code still crashes then the text field ProfitLabel is not connected in Interface Builder.
Side notes:
Please name variables according to the naming convention with starting lowercase letter.
Delete beginUpdates/endUpdates. Both lines have no effect.

ProfitLabel.text = NSString(format: "%.2f", outputValue) as String
it will round off up to 2 decimal places convert double into the string

You are force unwrapping the values. Replace following code
let priceValue = Double(priceTextField.text!)
let saleValue = Double(salesTextField.text!)
if priceValue != nil && saleValue != nil {
let outputValue = Double (saleValue! - priceValue!)
ProfitLabel.text = (outputValue)
}
With
if let price = Double(priceTextField.text ?? ""), let sale = Double(salesTextField.text ?? "") {
let outputValue = price - sale
ProfitLabel.text = String(outputValue)
} else {
print("Getting nil for \(priceTextField.text) or \(salesTextField.text)")
}

if priceValue != nil && saleValue != nil {
let profit = Double(saleValue)! - Double(priceValue)!
ProfitLabel.text = profit
}

Related

Error when trying to save with user defaults

Im having issues saving the table view data to user defaults, im getting the error " Cannot assign value of type '[String]' to type '[ViewController.ItemRow]' " in the load function. It gives the error at the line "items = loadedData". Does anyone know why its giving me this error!I dont know if im even doing it right.
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 ProfitLabel: RoundedLabel2!
#IBOutlet weak var TotalLabel: RoundedLabel2!
#IBOutlet weak var itemTextField: UITextField!
#IBOutlet weak var priceTextField: UITextField!
#IBOutlet weak var saleTextField: UITextField!
var items: [ItemRow] = []
struct ItemRow
{
var first: String
var second: String
var third: String
}
override func viewDidLoad() {
super.viewDidLoad()
load()
// tableView.tableFooterView = UIView(frame: CGRect.zero)
}
func save(){
UserDefaults.standard.set(items, forKey: "saved")
}
func load(){
if let loadedData:[String] = UserDefaults.standard.value(forKey: "saved") as? [String]{
items = loadedData
tableView.reloadData()
}
}
#IBAction func addButtonTapped(_ sender: Any) {
insertNewItems()
save()
// profit()
//
}
// func profit(){
// let priceValue = Double(priceTextField.text!)
// let saleValue = Double(saleTextField.text!)
//
// if priceValue != nil && saleValue != nil {
//
// let outputValue = Double (priceValue! - saleValue!)
//
// ProfitLabel.text = "\(outputValue)"
// }
// }
func insertNewItems() {
let newVideoRow = ItemRow(first: itemTextField.text!, second: priceTextField.text!, third: saleTextField.text!)
items.append(newVideoRow)
let indexPath = IndexPath(row: items.count - 1, section: 0)
tableView.beginUpdates()
tableView.insertRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
itemTextField.text = ""
priceTextField.text = ""
saleTextField.text = ""
view.endEditing(true)
}
}
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.first
cell.priceLabel.text = ItemTitle.second
cell.saleLabel.text = ItemTitle.third
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.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
save()
}
}
}
Make your struct conform to the Codable protocol. This is as simple as changing one line to:-
struct ItemRow : Codable
Your save function can be:-
if let data = try? PropertyListEncoder().encode(items) {
UserDefaults.standard.set(data, forKey: "SavedItems")
}
And load can be:
let defaults = UserDefaults.standard
if let data = defaults.data(forKey: "SavedItems") {
items = try! PropertyListDecoder().decode([ItemRow].self, from: data)
tableView.reloadData()
}
A quick look around SO the opinion seems to be that this shouldn't be used for large data sets. So this will get you started, but it may be worth considering a solution a bit more 'database-y' if you need larger amounts of data (e.g. CoreData or Realm).

Why my table view disappears after scrolling to the bottom?

I don't know why my table view disappears after I reach the bottom of my table view.
here is the gif file of my problem: http://g.recordit.co/4hizPCyctM.gif
here is my code in my view controller
class CheckoutVC: UIViewController {
#IBOutlet weak var orderButton: DesignableButton!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var floatingView: UIView!
#IBOutlet weak var totalPriceLabel: UILabel!
private let realm = RealmService.shared.realm
private var products = [Product]()
private var userOrder : Order?
private var productSelected : Product?
private var cartIsEmpty = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
RealmService.shared.observerRealmErrors() { (error) in
self.showAlert(alertTitle: "Sorry", alertMessage: error?.localizedDescription ?? "", actionTitle: "OK")
}
userOrder = Order.getOrderFromRealmDatabase()
guard let userOrder = userOrder else {return}
products = Array(userOrder.products)
tableView.reloadData()
totalPriceLabel.text = "Total: \(userOrder.getTotalPriceFormattedWithSeparator())"
updateUI()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
RealmService.shared.stopObservingErrors(in: self)
}
private func updateUI() {
guard let userOrder = userOrder else {return}
if userOrder.products.isEmpty {
tableView.isHidden = true
cartIsEmpty = true
orderButton.setTitle("Pilih Barang", for: .normal)
} else {
tableView.isHidden = false
cartIsEmpty = false
orderButton.setTitle("Pesan Barang", for: .normal)
}
}
}
//MARK: - Table View Delegate & Datasource
extension CheckoutVC : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userOrder?.products.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: CheckOutStoryboardData.TableViewIdentifiers.checkOutCell.rawValue, for: indexPath) as? CheckOutCell else {return UITableViewCell()}
guard let userOrderInRealm = userOrder else {return UITableViewCell()}
let products = userOrderInRealm.products
cell.productData = products[indexPath.row]
cell.indexPath = indexPath
cell.delegate = self
let stepperValue = Double(products[indexPath.row].quantity)
cell.stepperValue = stepperValue
return cell
}
}
and here is my code in the table view cell
class CheckOutCell: UITableViewCell {
var indexPath: IndexPath?
var delegate: CheckOutCellDelegate?
#IBOutlet weak var stepperGM: GMStepper!
#IBOutlet weak var productImageView: UIImageView!
#IBOutlet weak var productNameLabel: UILabel!
#IBOutlet weak var subCategoryNameLabel: UILabel!
#IBOutlet weak var pricePerUnitLabel: UILabel!
#IBOutlet weak var priceTotalPerItemLabel: UILabel!
var productData : Product? {
didSet {
updateUI()
}
}
var stepperValue : Double? {
didSet {
setStepper()
}
}
#IBAction func deleteButtonDidPressed(_ sender: Any) {
// send data to CheckoutVC
guard let indexPath = self.indexPath else {return}
self.delegate?.deleteButtonDidTapped(at: indexPath)
}
#IBAction func seeProductButtonDidPressed(_ sender: Any) {
// send data to CheckoutVC
guard let indexPath = self.indexPath else {return}
self.delegate?.viewProductButtonDidTapped(at: indexPath)
}
#IBAction func GMStepperDidTapped(_ sender: GMStepper) {
guard let indexPath = self.indexPath else {return}
let stepperValue = Int(sender.value)
self.delegate?.incrementOrDecrementButtonDidTapped(at: indexPath, counterValue: stepperValue)
}
func setStepper() {
guard let stepperValue = stepperValue else {return}
stepperGM.value = stepperValue
}
func updateUI() {
guard let productData = productData else {return}
// update UI
productNameLabel.text = productData.name
pricePerUnitLabel.text = productData.getFormattedUnitPriceWithSeparator()
priceTotalPerItemLabel.text = productData.getFormattedTotalPriceWithSeparator()
//set image
if let imagePath = productData.imagePaths.first {
guard let encodedImagePath = imagePath.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {return}
guard let url = URL(string: encodedImagePath) else {return}
productImageView.kf.indicatorType = .activity //loading indicator
productImageView.kf.setImage(with: url, options: [.transition(.fade(0.2))])
}
}
}
and here is the code to get the data from realm database, I get the data from realm database synchronously.
static func getOrderFromRealmDatabase() -> Order {
let userID = "1"
let realmService = RealmService.shared.realm
let allOrder = realmService.objects(Order.self)
let theOrder = allOrder.filter("userID CONTAINS[cd] %#", userID).first
if let userOrder = theOrder {
return userOrder
} else {
// Order never setted up before in Realm database container
// then create Order in realm database
let newOrder = Order()
newOrder.userID = userID
newOrder.products = List<Product>()
RealmService.shared.save(object: newOrder)
return newOrder
}
}
what went wrong in here, I don't understand :(
Remove optional handling in numberOfRowsInSection because products count never 0. and tableview hidden code is never excuted.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userOrder?.products.count
}

Load/Download image from SDWebimage and display in Custom tableView cell imageView

i already parsed other values but how will i show image to imageView i m already using SdWebImage in Swift.I want to show that in Bottom cell "detail" Below is my code
import UIKit
import SystemConfiguration
import MBProgressHUD
public struct Section {
var arrayDataTop: String
var arrayTerms: String
var qrImage:String
var collapsed: Bool
public init( arrayDataTop: String,qrImage: String ,arrayTerms: String, collapsed: Bool = false) {
self.arrayDataTop = arrayDataTop
self.qrImage = qrImage
self.arrayTerms = arrayTerms
self.collapsed = collapsed
}
}
class CollapsibleViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var tableViewCollapsible:UITableView!
#IBOutlet weak var listImage:UIImageView!
var nodatastr:String = "No Deal Found."
var dealIDCollapsible : String?
var dealDictCollapsible = [String: AnyObject]()
var parentNavigationController: UINavigationController?
private var loadingView:MBProgressHUD?
var sectionDataObj = [Section]()
override func viewDidLoad() {
super.viewDidLoad()
if (!self.isInternetAvailable()){
self.alertMessageShow(title: "No Internet Connection", message: "Make sure your device is connected to the internet.")
}
else{
if self.loadingView == nil {
self.loadingView = MBProgressHUD.showAdded(to: self.view, animated: true)
}
tableViewCollapsible.estimatedRowHeight = 100.0
tableViewCollapsible.layoutIfNeeded()
tableViewCollapsible.updateConstraintsIfNeeded()
tableViewCollapsible.tableFooterView = UIView()
self.listImageFetch()
dealFetchParticularListing()
}
}
func dealFetchParticularListing(){
let prs = [
"listing_id":dealIDCollapsible,//dealIDCollapsible,
"Deal_fetch_listing": "1" as String
]
Service.CreateDeal(prs as [String : AnyObject]?, onCompletion: { result in
let json = result as? NSDictionary
if let data = json as? [String:Any]{
if let err = data["status"] as? String, err == "success"{
if let data = data["result"] as? [Any]{
//
//fill your data in that local Section obj
//
var sectionDataObj = [Section]()
for sectionObj in data{
if let sectionObjVal = sectionObj as? [String:Any]{
if let qrcode = sectionObjVal["qrcode"] as? String{
if let tnc = sectionObjVal["tnc"] as? String{
if let deal_title = sectionObjVal["deal_title"] as? String{
let sectionValue = Section(arrayDataTop: deal_title, qrImage: qrcode, arrayTerms: tnc)
// access main objects/UIelement on main thread ONLY
sectionDataObj.append(sectionValue)
}
}
}
}
}
DispatchQueue.main.async { () -> Void in
self.sectionDataObj.removeAll()
//
//assign ur data in main sampleData(Section obj) then reload tableView with that data.
//
self.sectionDataObj = sectionDataObj
self.tableViewCollapsible.reloadData()
self.loadingView?.hide(true)
}
}
}
}
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Header
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! MyCellData
cell.lblDealTitle.text = sectionDataObj[indexPath.section].arrayDataTop
return cell
}
// Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "detail") as! MyCellData
cell.lblTerm.text = sectionDataObj[indexPath.section].arrayTerms
// here i want to show image
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension//320.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
let collapsed = !sectionDataObj[indexPath.section].collapsed
// Toggle collapse
sectionDataObj[indexPath.section].collapsed = collapsed
self.tableViewCollapsible.reloadSections([indexPath.section], with: .automatic)
}
}
}
class MyCellData:UITableViewCell{
#IBOutlet weak var lblDealTitle: UILabel!
#IBOutlet weak var dealimage: UIImageView!
#IBOutlet weak var lblTerm: UILabel!
#IBOutlet weak var qrCodeImage: UIImageView!
}
Plz help me with this.thanks in advance.All things are working properly i am able to see other details.Help will be appreciated.Plz help me i m struggling with this issue.
update ur tableView cellForRowAt like so to show an image or ur cell from ur sectionDataObj
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Header
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! MyCellData
cell.lblDealTitle.text = sectionDataObj[indexPath.section].arrayDataTop
return cell
}
// Cell
let cell = tableView.dequeueReusableCell(withIdentifier: "detail") as! MyCellData
cell.lblTerm.text = sectionDataObj[indexPath.section].arrayTerms
let imgUrl = sectionDataObj[indexPath.section]. qrImage
cell.qrCodeImage.sd_setImage(with: URL(string:imgUrl), completed: nil)
return cell
}

Update initialized Data in array with variable then pass array to next view controller

I'm having issues moving the data from the selected cells from the (service2viewcontroller) to the (confirmorderviewcontroller). I am trying to move the cell data (cells with a stepper.value above 0(var quantity > 0.0 (in Service2ViewController))), I was told to pass the array to the next view controller, to do so for a stepper value above 0 I would need to also send the indexpath.row for the rows with a quantity variable above 0 correct? I don't know how to do this if anyone can help I would greatly appreciate it. also the label is not updating when I use the stepper it stays at 0, can I place the quantity variable inside of the array? the total price label in the view controller continues to function and the data is sent to the (confirmorderviewcontroller) with no issues.
first TableView (data is input and forwarded)
class Service2ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var service2Total: UILabel!
#IBOutlet weak var service2TableView: UITableView!
// service data
var Wash: [Service2] = []
//stepper and price calculation
var quantity = Double()
var totalPrice : Double = 0.0
var priceList = [Int : Double]()
var totalProductPrice = [Int : Double]()
var label : Int!
override func viewDidLoad() {
super.viewDidLoad()
Wash = Options2()
if Int(quantity) > 0{
service2TableView.reloadData()
}
priceList[0] = 3.51//price list
priceList[1] = 5.51
service2Total.text = "$0.00"
}
// create data array
func Options2() -> [Service2]{
var washOptions: [Service2] = []
let option1 = Service2(titled: "Test", pricing: "$3.51", image: #imageLiteral(resourceName: "Wash&Fold"), description:"Testing the description box", quantity: Int(quantity))
let option2 = Service2(titled: "Test", pricing: "$5.51", image: #imageLiteral(resourceName: "Wash&Fold"), description: "Testing the description box", quantity: Int(quantity))
washOptions.append(option1)
washOptions.append(option2)
return washOptions
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Wash.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let Wash1 = Wash[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Service2Cell", for: indexPath) as! Service2TableViewCell
cell.setService(Wash: Wash1)
cell.selectionStyle = .none
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 133
}
#IBAction func stepperAcn(_ sender: UIStepper) {
//change label value with stepper
let cellPosition = sender.convert(CGPoint.zero, to: service2TableView)
let indPath : IndexPath = service2TableView.indexPathForRow(at: cellPosition)!
quantity = sender.value
let getCurrentProductPrice : Double = priceList[indPath.row]! * sender.value
totalPrice = gettingPriceLabel(indPath: indPath, getCurrentProductPrice: getCurrentProductPrice)
if totalPrice == 0{
service2Total.text = ("$0.00")
}
else{
service2Total.text = ("$")+String(totalPrice)
}
print("total price",totalPrice)
print("quantity double",quantity)
service2TableView.reloadData()
}
func gettingPriceLabel(indPath: IndexPath, getCurrentProductPrice : Double) -> Double
{
totalProductPrice[indPath.row] = getCurrentProductPrice
var totalCost : Double = 0.0
let valuesArr = Array(totalProductPrice.values)
for i in 0..<valuesArr.count
{
totalCost = totalCost + valuesArr[i]
}
return totalCost
}
// add function to collect (didSelectRowAt) and send selected data to cart and prepare for segue
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
// change sender to
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let DestViewController: ConfirmorderViewController = segue.destination as! ConfirmorderViewController
if totalPrice > 0.00{
DestViewController.amount = totalPrice
}
}
}
service initializer
class Service2
{
var service2Title: String
var service2Image: UIImage
var Service2Pricing: String
var service2Description: String
var service2Quantity: Int
init(titled: String, pricing: String, image: UIImage, description: String, quantity: Int){
self.service2Title = titled
self.Service2Pricing = pricing
self.service2Image = image
self.service2Description = description
self.service2Quantity = quantity
}
}
Service 2 TableViewCell
class Service2TableViewCell: UITableViewCell {
#IBOutlet weak var service2Title: UILabel!
#IBOutlet weak var service2Stepper: UIStepper!
#IBOutlet weak var service2StepperLbl: UILabel!
#IBOutlet weak var service2Pricing: UILabel!
#IBOutlet weak var service2Image: UIImageView!
#IBOutlet weak var service2Description: UILabel!
func setService(Wash: Service2){
service2Image.image = Wash.service2Image
service2Pricing.text = Wash.Service2Pricing.description
service2Title.text = Wash.service2Title
service2Description.text = Wash.service2Description
service2StepperLbl.text = Wash.service2Quantity.description
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Second TableView (receives data)
class ConfirmorderViewController: UIViewController{
#IBOutlet weak var Total: UILabel!
#IBOutlet weak var confirmOrderTableView: UITableView!
var titled = [String]()
var amount: String = ""
//var quantity = String()
var image1 = [UIImage]()
var Price = [Double]()
override func viewDidLoad() {
super.viewDidLoad()
Total.text = amount
confirmOrderTableView.reloadData()
}
}
extension ConfirmorderViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titled.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConfirmOrderTableViewCell") as! ConfirmOrderTableViewCell
cell.coTitle?.text = titled[indexPath.row]
cell.coImg?.image = image1[indexPath.row]
//cell.coQuantity.text = quantity
cell.coPrice?.text = Price.description
return cell
}
}
I have tried here. I got list of row numbers having more than 0 order. I have it stored in whichRowToBeAdd. If user decreased to Zero, respective rows will removed from this array.
With the help of Singleton Class, we can store whatever we need to show in NextViewController
var whichRowToBeAdd = [Int]() // GLOBAL
#IBAction func stepperAcn(_ sender: UIStepper) {
//change label value with stepper
let cellPosition = sender.convert(CGPoint.zero, to: service2TableView)
let indPath : IndexPath = service2TableView.indexPathForRow(at: cellPosition)!
if Int(sender.value) == 0
{
if whichRowToBeAdd.contains(indPath.row)
{
whichRowToBeAdd.remove(at: whichRowToBeAdd.index(of: indPath.row)!)
}
else
{
}
}
else
{
if whichRowToBeAdd.contains(indPath.row)
{
}
else
{
whichRowToBeAdd.append(indPath.row)
}
}
//....
//..... Your Code in your answer
}
// After stepper Action, final click of Button, which moves to Next ViewController
#IBAction func goToConfirmOrder(_ sender: UIBarButtonItem) {
print("\n\n Val_ ", whichRowToBeAdd)
singleTon.sharedInstance.orderDict.removeAll()
for i in 0..<whichRowToBeAdd.count
{
let indPath = IndexPath(row: whichRowToBeAdd[i], section: 0)
let newCell = tblVw.cellForRow(at: indPath) as! Service2TableViewCell
print("qweqwe ",newCell.testLbl.text)
let name : String = newCell.service2Title.text!
let image : UIImage = newCell.service2Image.image
let quantity : Int = Int(newCell.service2StepperLbl.text!)!
getOrderOneByOne(productName: name, productImage: image, productQuantity: quantity)
if i == (whichRowToBeAdd.count - 1)
{
self.performSegue(withIdentifier: "confirmOrderVC", sender: nil)
}
}
}
func getOrderOneByOne(productName: String, productImage : UIImage, productQuantity: Int)
{
let createDict = ["productName" : productName, "productImage" : productImage, "productQuantity" : productQuantity] as [String : Any]
singleTon.sharedInstance.orderDict.append(createDict)
}
Singleton Class
class singleTon: NSObject {
static let sharedInstance = singleTon() // Singleton Instance
var orderDict = [[String : Any]]() // Dictionary Declaration
}
Next ViewController
class ConfirmOrderViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("\n\norderDict.coun ", singleTon.sharedInstance.orderDict)
}
}
With this, you can display datas in TableView in this ConfirmOrderViewController.

how to insert data from string to Sqlite in Swift3?

I have a problem that I have tried to insert string data to Sqlite. I want to do is in my Details ViewController if user press Like button, the word and meaning of string pass into Sqlite file that show in Favourite ViewController. Below is my DetailsVC code.
import UIKit
import AVFoundation
class DetailsVC: UIViewController, UITextViewDelegate {
#IBOutlet weak var wordLbl: UILabel!
#IBOutlet weak var meaningLbl: UITextView!
#IBOutlet weak var sysWordLbl: UILabel!
#IBOutlet weak var sysWordsLbl: UILabel!
#IBOutlet weak var anyWordsLbl: UILabel!
#IBOutlet weak var anyWordLbl: UILabel!
#IBOutlet weak var wordImg: UIImageView!
#IBOutlet weak var buSound: UIButton!
#IBOutlet weak var buLike: UIButton!
#IBOutlet weak var menuBtn: UIButton!
//Data from HomeVC
var data:Data?
var fdatas = [FData]()
var favouriteVC = FavouriteVC()
override func viewDidLoad() {
super.viewDidLoad()
meaningLbl.delegate = self
wordLbl.text = "\((data?._word.capitalized)!) \((data?._phonetic)!)"
self.meaningLbl.text = data?._meaning
self.sysWordsLbl.text = data?._meaning
self.anyWordsLbl.text = data?._meaning
self.sysWordLbl.text = "Synonym"
self.anyWordLbl.text = "Antonym"
meaningLbl.font = UIFont(name: FONT_REGULAR, size: 24.0)
}
#IBAction func buMenu(_ sender: Any) {
//menu
dismiss(animated: true, completion: nil)
}
#IBAction func buSound(_ sender: Any) {
//Todo: speak the word
if buSound.isEnabled == true{
buSound.setImage(UIImage(named: "volume-2.png"), for: .normal)
}else{
buSound.setImage(UIImage(named: "volume-un-2.png"), for: .normal)
}
let utTerance = AVSpeechUtterance(string: "\((data?._word)!)")
let synthesize = AVSpeechSynthesizer()
utTerance.voice = AVSpeechSynthesisVoice(language: "en-gb")
synthesize.speak(utTerance)
}
#IBAction func buLike(_ sender: Any) {
//Todo: Click Like
if buLike.isEnabled == true{
buLike.setImage(UIImage(named: "heart.png"), for: .normal)
//Save data to Database
let word = data?._word ?? ""
let meaning = data?._meaning ?? ""
do{
let favData = FData(word: word, meaning: meaning)
self.fdatas.append(favData)
print("\(word), \(meaning)")
}catch{
print(error)
}
}else{
buSound.setImage(UIImage(named: "heart-un.png"), for: .normal)
}
//FavouriteVC.insertRowsAtIndexPaths([NSIndexPath(forRow: fdatas.count-1, inSection: 0)], withRowAnimation: .Fade)
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
self.view.endEditing(true);
return false;
}
}
My FavouriteVC
import UIKit
import SwipeCellKit
import SQLite
class FavouriteVC: UIViewController,UITableViewDelegate,UITableViewDataSource, SwipeTableViewCellDelegate{
//TabelView
#IBOutlet weak var fTableView: UITableView!
//Variables
var fdata = [FData]()
override func viewDidLoad() {
super.viewDidLoad()
// FtableView
fTableView.delegate = self
fTableView.dataSource = self
//reload Data
fTableView.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fdata.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: FAVOURITE_CELL, for: indexPath) as? FavouriteCell{
cell.configureCell(fdata: fdata[indexPath.row])
//cell.delegate = self
return cell
}
return FavouriteCell()
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
// handle action by updating model with deletion
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete")
return [deleteAction]
}
}
FData.swif
import Foundation
class FData {
//let id: Int64?
var word: String
var meaning: String
//var address: String
init(id: Int64) {
// self.id = id
word = ""
meaning = ""
//address = ""
}
init(word: String, meaning: String) {
// self.id = id
self.word = word
self.meaning = meaning
//self.address = address
}
}
FavouriteData Instance
import UIKit
import SQLite
class FavouriteData{
static let instance = FavouriteData()
private let db: Connection?
private let words = Table("words")
private let id = Expression<Int64>("id")
private let wordL = Expression<String?>("word")
private let meaningL = Expression<String?>("shanword_uni")
//private let address = Expression<String>("address")
private init() {
let path = NSSearchPathForDirectoriesInDomains(
.documentDirectory, .userDomainMask, true
).first!
do {
db = try Connection("\(path)/favourite.sqlite")
} catch {
db = nil
print ("Unable to open database")
}
createTable()
}
func createTable() {
do {
try db!.run(words.create(ifNotExists: true) { table in
table.column(id, primaryKey: true)
table.column(wordL)
//table.column(phone, unique: true)
table.column(meaningL)
})
} catch {
print("Unable to create table")
}
}
func addData(word: String, meaning: String) -> Int64? {
do {
let insert = words.insert(wordL <- word, meaningL <- meaning)
let id = try db!.run(insert)
print("Save: \(id)")
return id
} catch {
print("Insert failed")
return -1
}
}
func getData() -> [FData] {
var contacts = [FData]()
do {
for contact in try db!.prepare(self.words) {
contacts.append(FData(
// id: contact[id],
word: contact[wordL]!,
meaning: contact[meaningL]!))
}
} catch {
print("Select failed")
}
return contacts
}
func deleteData(index: Int64) -> Bool {
do {
let contact = words.filter(id == index)
try db!.run(contact.delete())
return true
} catch {
print("Delete failed")
}
return false
}
}
But, there is not cell of row in the FavouriteVC. How to fix this? Please help me.
Best,
Sai Tawng Pha
I have solved my problem by add one line of code. In my FavouriteVC class, just add it under ViewdidLoad function " fdata = FavouriteData.instance.getData() ".
Here is my complete code.
import UIKit
import SwipeCellKit
import SQLite
class FavouriteVC: UIViewController,UITableViewDelegate,UITableViewDataSource, SwipeTableViewCellDelegate{
//TabelView
#IBOutlet weak var fTableView: UITableView!
//Variables
var fdata = [FData]()
override func viewDidLoad() {
super.viewDidLoad()
// FtableView
fTableView.delegate = self
fTableView.dataSource = self
//reload Data
fTableView.reloadData()
//Get Data
fdata = FavouriteData.instance.getData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return fdata.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: FAVOURITE_CELL, for: indexPath) as? FavouriteCell{
cell.configureCell(fdata: fdata[indexPath.row])
//cell.delegate = self
return cell
}
return FavouriteCell()
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
// handle action by updating model with deletion
}
// customize the action appearance
deleteAction.image = UIImage(named: "delete")
return [deleteAction]
}
}

Resources