删除单元格后更新标签

时间:2018-07-04 04:22:06

标签: ios arrays swift uitableview uilabel

首先,对于我的标题与我的问题相比是否有误导性,我深表歉意。我不太确定该如何说出来。希望我的照片比我做更多的解释。我仍然处于iOS开发的初期,似乎遇到了第一个问题。基本上,我正在创建一个应用程序,人们可以输入他们购买的物品,然后再出售,然后他们可以跟踪自己的利润/亏损。

基本上,用户可以添加一个商品,如下图所示,然后它将使用商品名称,通过该交易获得或失去的金额以及有关该商品的其他信息来填充表格视图>

Pic 1

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

Pic 2

Pic 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

2 个答案:

答案 0 :(得分:0)

正如@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()
        }
    }

答案 1 :(得分:0)

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

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