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