从tableView删除StructItem

时间:2018-08-13 19:25:40

标签: swift uitableview struct delegates

我希望能够删除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)

    }

1 个答案:

答案 0 :(得分:0)

您必须重新加载表格视图

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)
}