Ios 删除单元格后更新标签

Ios 删除单元格后更新标签,ios,arrays,swift,uitableview,uilabel,Ios,Arrays,Swift,Uitableview,Uilabel,首先,如果我的标题与我的问题相比有点误导,我道歉;我真的不知道该怎么说。希望我的照片能比我做更多的解释。我仍然处于iOS开发的初始阶段,我似乎遇到了我的第一个问题。基本上,我正在创建一个应用程序,用户可以输入他们购买的商品,然后在以后出售,他们可以跟踪他们的利润/损失 基本上,用户可以添加如下图所示的项目,然后继续使用项目标题、他们通过该交易获得或损失的金额以及关于该项目的其他信息填充tableview 其次,我有一个功能,用户可以通过向左滑动从单元格中删除项目。我的第一个问题是数量(即3)和

首先,如果我的标题与我的问题相比有点误导,我道歉;我真的不知道该怎么说。希望我的照片能比我做更多的解释。我仍然处于iOS开发的初始阶段,我似乎遇到了我的第一个问题。基本上,我正在创建一个应用程序,用户可以输入他们购买的商品,然后在以后出售,他们可以跟踪他们的利润/损失

基本上,用户可以添加如下图所示的项目,然后继续使用项目标题、他们通过该交易获得或损失的金额以及关于该项目的其他信息填充tableview

其次,我有一个功能,用户可以通过向左滑动从单元格中删除项目。我的第一个问题是数量(即3)和总金额(“169.82美元”)标签在删除单元格后不会立即更新。我的第二个问题是总量标签本身;我可以通过检索存储Items对象的数组的计数来更新quantity标签,但我无法使用total amount标签

下面是我的代码片段

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)
以下是我对解决方案的尝试:

       /* 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()
    }
}
代码的其余部分

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

正如@Kamran所说。删除单元格后需要重新计算

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()
        }
    }

只需创建一个方法。其中,您必须计算管理阵列中的衣服总数和数量。并在每次修改列表时调用该函数

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

您需要管理源类以更新标签、类/字典/数组(用于首次填充数量)、删除tableCell后更新受尊重的类/字典/数组并仅刷新标签您认为我尝试的解决方案是否正确?我知道我需要做什么,但只是不知道怎么做,因为我的解决方案总是会出现“零”错误。再次计算
ClothesTotalAmount
,并更新标签,就像你在
viewDidLoad
中所做的一样;那么我的计算中一定遗漏了什么?因为我假设你只需要从总金额中减去已删除单元格中的金额,对吗?不需要删除。删除已删除的值后,只需再次对数组中的值求和。这更干净,因为总值将始终反映数组的当前状态;我相信我知道我的问题是什么:这是我如何加我的衣服总数。我相信我知道如何修复它,但谢谢你的帮助!您为修复@Sweetcharge添加的代码。您告诉我上述代码不起作用。”