gpt4 book ai didi

swift - 从 tableView 中删除 StructItem

转载 作者:行者123 更新时间:2023-11-30 11:14:59 25 4
gpt4 key购买 nike

我希望能够删除 tableView 中的行/项目。
tableView 填充有 struct
结构:

struct Items {
var name: String = ""
var count = Int()
}

var itemsInStore : [Items] = []

tableView的填充如下:

  public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CheckOutCell") as! CheckOutTableViewCell


let name = itemsInStore[indexPath.row].name
let count = itemsInStore[indexPath.row].count

cell.checkOutLabel.text = "\(count) Pcs \(name)"

cell.delegate = self

return cell
}

按钮:
我一直在尝试不同的版本,但还没有成功:

extension CheckOutViewController: RemoveItemDelegate
{
func didDeleteItemCell(cell: CheckOutTableViewCell) {

if let index = itemsInStore.index(where: {$0.name == //Index for the row that is to be deleted }) {
itemsInCart.remove(at: index)

}

最佳答案

您必须重新加载表格 View

if let index = itemsInStore.index(where: {$0.name == //Index for the row that is to be deleted })   {
itemsInCart.remove(at: index)
tableView.reloadData()
}

或者 - 更好 - 删除带有动画的特定行

if let index = itemsInStore.index(where: {$0.name == //Index for the row that is to be deleted })   {
itemsInCart.remove(at: index)
let indexPath = IndexPath(row: index, section: 0)
tableView.deleteRows(at: [indexPath], with: .fade)
}

关于swift - 从 tableView 中删除 StructItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51829266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com