简体   繁体   中英

Updating a label after deleting a cell

First and foremost, I apologize if my title is kind of misleading compared to my question; I wasn't really sure how to word it out. Hopefully my pictures do more explanation than me. Im still at the beginning stages of iOS development and I seem to have run into my first problem. Basically, I'm creating an app where a person can input item's they have purchased and then later on sold and they can keep track of their profits/losses.

Basically, a user can add an item like the image below and it will then proceed to populate the tableview with the item title, how much they gained or lost through that transaction, and other pieces of information about that item

Pic 1

Second, I have a feature where a user can delete an item from the cell by swiping left. My first problem is that the quantity (Ie 3) and total amount ("$169.82") labels don't update instantly after the deletion of the cell. My second problem is the total amount label itself; I'm able to update the quantity label by simply retrieving the count of the array where the Items Objects are stored in but I'm unable to do so with the total amount label

Pic 2

Pic 3

Here is a snippet of my code

import UIKit

var ClothesList = [String]()
var ClothesResults = [String]()
var ClothesTotalQty = AccessoryList.count
var ClothesBuy = [String]()
var ClothesSell = [String]()
var ClothesFeeArray = [String]()
var ClothesSize = [String]()
var ClothesTotalAmount = 0.0

class ViewClothes: UIViewController, UITableViewDelegate, 
UITableViewDataSource {

// MARK: Outlets

@IBOutlet weak var ClothesQuantity: UILabel!
@IBOutlet weak var ClothesAmount: UILabel!
@IBOutlet weak var ClothesNames: UITableView!

// MARK: Functions

func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int {
    return ClothesList.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
    let list = ClothesNames.dequeueReusableCell(withIdentifier: 
"Clothes") as! CustomCells
    list.NameLabel?.text = ClothesList[indexPath.row]
    list.BuyPriceLabel?.text = "Buy Price: $\ 
(ClothesBuy[indexPath.row])"
    list.FeeLabel?.text = "Fee: \(ClothesFeeArray[indexPath.row])%"
    list.SizeLabel?.text = "Size: \(ClothesSize[indexPath.row])"
    list.SellLabel?.text = "Sell Price: $\ 
(ClothesSell[indexPath.row])"
    list.ModifiedProfitLabel?.text = ClothesResults[indexPath.row]


    return list
}

func tableView(_ tableView: UITableView, commit editingStyle: 
UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if(editingStyle==UITableViewCellEditingStyle.delete){
        ClothesList.remove(at: indexPath.row)

And here is my attempt at the solution:

       /* My Attempt at subtracting the removed cell from the total 
amount
        let placeholder = ClothesResults[indexPath.row]
        ClothesTotalAmount = ClothesTotalAmount - Double(placeholder)!
        ClothesResults.remove(at: indexPath.row) */

        ClothesTotalQty = ClothesList.count
        ClothesNames.reloadData()
    }
}

Rest of the code

override func viewDidAppear(_ animated: Bool) {
    ClothesNames.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.shadowImage = UIImage()

    ClothesNames.delegate = self
    ClothesNames.dataSource = self


    ClothesQuantity.text = String(ClothesTotalQty)
    let totalAmount = ClothesTotalAmount as NSNumber
    let totalString = currencyFormatter.string(from: totalAmount)
    ClothesAmount.text = totalString

simply create a method. In which you have to calculate the total amount and count of clothes from your manged array . And call the function each time you modify the list.

if(editingStyle==UITableViewCellEditingStyle.delete){  RECALCULATE YOUR AMOUNT AND CLOTHES COUNT HERE OR CALL A FUNCTION FOR SAME ClothesNames.reloadData() } }

As @Kamran said . You need to recalculate after deleting cell

func tableView(_ tableView: UITableView, commit editingStyle: 
    UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if(editingStyle==UITableViewCellEditingStyle.delete){
            ClothesList.remove(at: indexPath.row)

        ClothesQuantity.text = String(ClothesTotalQty)
        let totalAmount = ClothesTotalAmount as NSNumber
        let totalString = currencyFormatter.string(from: totalAmount)
        ClothesAmount.text = totalString

            ClothesTotalQty = ClothesList.count
            ClothesNames.reloadData()
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM