ios - 删除单元格后更新标签
问题描述
首先,如果我的标题与我的问题相比具有误导性,我深表歉意;我真的不知道如何说出来。希望我的图片能比我做更多的解释。我仍处于 iOS 开发的初级阶段,我似乎遇到了我的第一个问题。基本上,我正在创建一个应用程序,人们可以在其中输入他们购买的物品,然后再出售,他们可以跟踪他们的利润/损失。
基本上,用户可以添加如下图所示的项目,然后它将继续使用项目标题、他们通过该交易获得或损失的金额以及有关该项目的其他信息来填充表格视图
其次,我有一个功能,用户可以通过向左滑动来从单元格中删除一个项目。我的第一个问题是数量(即 3)和总金额(“$169.82”)标签在删除单元格后不会立即更新。我的第二个问题是总量标签本身;我可以通过简单地检索存储 Items 对象的数组的计数来更新数量标签,但我无法使用总量标签这样做
这是我的代码片段
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
解决方案
只需创建一个方法。在其中你必须从你的管理数组中计算衣服的总数和数量。并在每次修改列表时调用该函数。
if(editingStyle==UITableViewCellEditingStyle.delete){ RECALCULATE YOUR AMOUNT AND CLOTHES COUNT HERE OR CALL A FUNCTION FOR SAME ClothesNames.reloadData() } }
推荐阅读
- scala - Scala:不可变的 Map 正在改变它的值
- angular - 在使用 AWS cognito 进行身份验证后,通过 Ionic4 + Angular 在 websockets 上使用 MQTT
- excel - 将不同的列堆叠成不同工作表上的一列
- numpy - 如何将 RGB 图像(1-D)的扁平数组转换回原始图像
- webserver - 如何将文件上传到 Microsoft Azure 虚拟机上的 LEMP 堆栈?
- python - 如何使用 python 2.7 检测红旗、网络钓鱼或恶意站点?
- go - 将结构转换为元组切片而不进行反射
- python - 如何根据来自另外两个数据框的值填充 Pandas 数据框
- java - 后映射中的 Json EOF 验证 (@requestbody)
- node.js - lodash 删除后保存数组