从表格视图中删除行

时间:2018-08-06 11:14:16

标签: ios swift uitableview

如何从表格视图单元格中删除行我无法从表格视图中删除单元格我不知道为什么,当我尝试从表格视图中删除永不消失时,请告诉我如何从多个单元格中删除一个单元格我有我知道我应该有var,但是我没有把它作为例子var = something

class addCartTblVC: UITableViewController {
//MARK: -Variables
var coreDataVariables = GetCoreDataVariables()

//MARK: -ViewMethords
override func viewDidLoad() {
    super.viewDidLoad()
    self.fetchCart()
    let backItem = UIBarButtonItem()
    backItem.title = "Back"
    navigationItem.backBarButtonItem = backItem
    tableView.tableFooterView = UIView()
}

//MARK: -tableView dataSource
override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return coreDataVariables.result_add_Cart.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cartCell = tableView.dequeueReusableCell(withIdentifier: "cartCell", for: indexPath) as! AddCartTblCell
    let img = coreDataVariables.result_add_Cart[indexPath.row].product_Image!
    let userPhotoString = img
    let imageUrl:URL = URL(string: userPhotoString)!
    let imageData:Data = try! Data(contentsOf: imageUrl)
    // Add photo to a cell as a subview

    if let image = UIImage(data: imageData){
         cartCell.pro_Img.image = image
    }
    cartCell.pro_MRP.text = coreDataVariables.result_add_Cart[indexPath.row].product_Price
    cartCell.pro_Details.text = coreDataVariables.result_add_Cart[indexPath.row].product_Name
    cartCell.pro_Discount.text = coreDataVariables.result_add_Cart[indexPath.row].product_Discount
    if cartCell.pro_Discount.text! == cartCell.pro_MRP.text!{
        cartCell.pro_Discount.isHidden = true
        cartCell.crossView.isHidden = true
    }
    else{
        cartCell.pro_MRP.textColor = UIColor.darkGray
        cartCell.pro_Discount.isHidden = false
        cartCell.crossView.isHidden = false
    }
    cartCell.proceedBtn.tag = indexPath.row
    cartCell.proceedBtn.addTarget(self, action: #selector(addCartTblVC.ProceedTapped(_:)), for: .touchUpInside)
    cartCell.removeFromCoreDataBtn.tag = indexPath.row
    cartCell.removeFromCoreDataBtn.addTarget(self, action: #selector(addCartTblVC.deleteFromCart(_:)), for: .touchUpInside)
    return cartCell
}


//MARK: -fetchFunction
func fetchCart(){
    let cart = Add_Cart()
    coreDataVariables.result_add_Cart = cart.fetchCart()!
}

//MARK: -Proceed Action
@IBAction func back(_ sender: UIBarButtonItem) {
    dismiss(animated: false, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

在加载表数据之前,请尝试在coreDataVariables.result_add_Cart上循环并检查重复元素,并尝试通过将重复的product_Price * n乘以重复元素使它们成为单个元素,并还要注意product_Discount。

您的方法应该是这样的((此方法仅用于删除重复项):

extension Array where Element: Equatable {
mutating func removeDuplicates() {
    var result = [Element]()
    for value in self {
        if !result.contains(value) {
            result.append(value)
        }
    }
    self = result
}

==>希望不要对这个答案投反对票: