【问题标题】:Updating a label after deleting a cell删除单元格后更新标签
【发布时间】:2018-12-12 10:12:14
【问题描述】:

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

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

Pic 1

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

Pic 2

Pic 3

这是我的代码的 sn-p

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

【问题讨论】:

  • 你需要管理源类来更新labels,Class/Dictionary/Array第一次用来填充Amount,Quantity,更新尊重的Class/Dictionary/Array 删除 tableCell 后刷新标签
  • 你会说我尝试的解决方案是否在正确的轨道上?我知道我需要做什么,但我不知道该怎么做,因为我的解决方案总是会出现“Nil”错误。
  • 再次计算ClothesTotalAmount 并像在viewDidLoad 中一样更新标签
  • 知道了;那么我的计算中一定遗漏了一些东西吗?因为我假设您只是从总金额中减去已删除单元格中的金额,对吗?
  • 不需要删除。删除已删除的值后,只需再次对数组中的值求和即可。这更清晰,因为总值将始终反映数组的当前状态。

标签: ios arrays swift uitableview uilabel


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 还是不行;我相信我知道我的问题是什么:这就是我将 ClothesTotalAmount 相加的方式。我相信我知道如何解决它,但感谢您的帮助!
    • 您为修复@Sweetcharge 添加的代码。你告诉上面的代码不起作用。'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    相关资源
    最近更新 更多