0

I have set a leading swipe action for tableview cell, which add to cart. I have not implemented any trailing swipe action method, still I am getting delete action for every cell. Below is my code of all tableView methods. I have also attached one image from simulator.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10

}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (indexPath.row == 0) {
        return 267
    }
    else {
        return 85
    }
}

//tableview methods

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// For 1st cell

    if (indexPath.row == 0){
    let cell = storeInfoTable.dequeueReusableCell(withIdentifier: "storeinfocell1", for: indexPath) as! CustomStoreInfoTableOneViewCell


        return cell

    }

// For rest of the cells

    let cell = storeInfoTable.dequeueReusableCell(withIdentifier: "storeItemCell", for: indexPath) as! StoreItemsTableViewCell
    cell.storeItemCellView.layer.cornerRadius = 5.0


    return cell[![enter image description here][1]][1]

}


@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    if (indexPath.row == 0) {return nil}

    let addToCart = UIContextualAction(style: .normal, title: "Add to Cart") { (action, view, nil) in
        print("Added to cart")
        let cell = self.storeInfoTable.cellForRow(at: indexPath) as! StoreItemsTableViewCell

        if (Int(cell.numberOfItem.text!)! == 0) {
            self.view.showToast(toastMessage: "Items must be atleast one", duration: 1.5)
        }
        else {
        self.view.showToast(toastMessage: "Successfully added to cart", duration: 1.5)
        }
    }
    addToCart.title = "Add to Cart"
    addToCart.backgroundColor = #colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)
    addToCart.image = UIImage(named: "cart")



    return UISwipeActionsConfiguration(actions: [addToCart])
}
Parth Rudakia
  • 151
  • 16

2 Answers2

6

Solution for this is to implement a trailing swipe action without any actions defined. code is below.

@available(iOS 11.0, *)
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    return UISwipeActionsConfiguration.init()
}
Parth Rudakia
  • 151
  • 16
0

still I am getting delete action for every cell

Typically, that's caused by having an implementation of tableView(_:commit:forRowAt:).

matt
  • 515,959
  • 87
  • 875
  • 1,141