Post

Replies

Boosts

Views

Activity

Reply to Create a func which totals the value of data
This code gives me the result of savings but is also adding deleted records to the total point? How do I prevent deleted records to be added to the total. import UIKit import CoreData class TotalPointsViewController: UIViewController { var points = [Aktie]()     @IBOutlet weak var totalPoints: UILabel!     override func viewDidLoad() {         super.viewDidLoad()                      let appDelegate = UIApplication.shared.delegate as! AppDelegate             let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext             let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")             do {             let points: NSArray = try context.fetch(request) as NSArray             var sum = 0             for result in points {                 sum += Int((result as AnyObject).point ?? "0") ?? 0             }             totalPoints.text = "\(sum)"         }         catch {             print("Fetch Failed")         }     } }
May ’22
Reply to UI Stepper implementation - start counting over
I have managed to add the following code to remember the value in the stepper. counterStepper.value=value counterTF.text=String(describing: value) I added the following code to the stepper. @IBAction func counterStepperPressed(_ sender: UIStepper) { counterTF.text=String(describing: sender.value) UserDefaults.standard.setValue(sender.value, forKey: "counterStepper") NotificationCenter.default.post(Notification.init(name: Notification.Name("StepperDidChangeValue"))) } The only issue I have is that if I edit a second item it remembers the value of the first item. Somehow it is not remembering the original value of the second item.
May ’22
Reply to Create a func which totals the value of data
I have tried the code in both answers - I also positioned the code where I calculate the total points between various brackets but it is not showing any results in my viewcontroller. There are no coding errors in both solutions. There is data in my records. import CoreData class TotalPointsViewController: UIViewController {     @IBOutlet weak var totalPoints: UITextField!     var points = [Aktie]()     var firstLoad = true     override func viewDidLoad() {         super.viewDidLoad()       if firstLoad         {             firstLoad = false             let appDelegate = UIApplication.shared.delegate as! AppDelegate             let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext             let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")             do {                 let results: NSArray = try context.fetch(request) as NSArray                 for result in results {                     let aktie = result as! Aktie                     points.append(aktie)                 }             }             catch             {                 print("Fetch Failed")             }       }   }       func calculateSumPoints() {         let sum = points.compactMap { $0.point }             .reduce(0) { previous, next in                 guard let value = Int(next) else { return previous }                 return previous + value             }         totalPoints.text = "\(sum)"     } }
May ’22
Reply to Seque is not working
I use the actionGoal in the data model as a section: // Table View Sections              func numberOfSections(in tableView: UITableView) -> Int {         return result?.data3.count ?? 0                          }         func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {             return result?.data3[section].actionGoal         } And then the tableView: // Table View              func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {         if let result = result {             return result.data3[section].actions.count         }             return 0 }         func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {             let action = result?.data3[indexPath.section].actions[indexPath.row]             let cell = ActionTableView.dequeueReusableCell(withIdentifier: "ActionCell", for: indexPath) as! ActionTableCell                 cell.ActionTitle.text = action?.actionTitle                 cell.ActionImage.image = UIImage(named: action!.actionImage)                   return cell              }         func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {             return 80     }          // This code is for the transfer to the next page                  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {                          index = 0                                      performSegue(withIdentifier: "showDetail", sender: self)        }         override func prepare(for segue: UIStoryboardSegue, sender: Any?) {             if let destination = segue.destination as? ActionDetailViewController {                 destination.action = result?.data3[index].actions[(tableView.indexPathForSelectedRow?.row)!]        }
Apr ’22