首页 > 解决方案 > 删除单元格后更新标签

问题描述

首先,如果我的标题与我的问题相比具有误导性,我深表歉意;我真的不知道如何说出来。希望我的图片能比我做更多的解释。我仍处于 iOS 开发的初级阶段,我似乎遇到了我的第一个问题。基本上,我正在创建一个应用程序,人们可以在其中输入他们购买的物品,然后再出售,他们可以跟踪他们的利润/损失。

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

图一

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

图二

图 3

这是我的代码片段

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

标签: iosarraysswiftuitableviewuilabel

解决方案


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

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

推荐阅读