0

My question about tableview cell deleting process. When I try to delete cell with button, tableview first cell is deleted because of let indexPath = IndexPath(row: 0, section: 0) How can I delete clicked button's cell? I can't find any way to take clicked row. Can anyone help me?

    @objc func deleteAction() {


    let indexPath = IndexPath(row: 0, section: 0)
    _ = tableView.cellForRow(at: indexPath)
        if countPriceArray[indexPath.row] >= 1 {
            let ord = arraysForFiyat[indexPath.row]
            let splitOrd = ord.components(separatedBy: " ₺")
            let intOrd = splitOrd.map{ Int($0) ?? 0 }
            sum = sum - intOrd[0]
            UserDefaults.standard.set(arraysForUrun, forKey: "urunadi")
            UserDefaults.standard.set(arraysForFiyat, forKey: "urunfiyat")
            tableView.reloadData()

            if countPriceArray[indexPath.row] > 1 {
                countPriceArray[indexPath.row] -= 1
                tableView.reloadData()

            }else if countPriceArray[indexPath.row] == 1 {
                arraysForUrun.remove(at: indexPath.row)
                arraysForFiyat.remove(at: indexPath.row)
                countPriceArray.remove(at: indexPath.row)
                UserDefaults.standard.set(arraysForUrun, forKey: "urunadi")
                UserDefaults.standard.set(arraysForFiyat, forKey: "urunfiyat")
                tableView.deleteRows(at: [indexPath], with: .fade)

            }

        }
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
eksglox
  • 41
  • 7
  • @thisIsTheFoxe I want to make it with button action commit editing code works I know it but I want to ask how can I take indexPath.row of clicked cell's button? – eksglox Sep 25 '19 at 13:20

1 Answers1

0

Swift 5

In Tableview delegate Method cellForRowAt

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
        cell.btnAction.tag = indexPath.row
        cell.btnAction.addTarget(self, action: #selector(buttonActionClicked(sender:)), for: .touchUpInside)
        return cell
    }

Method of Button Action

@objc func buttonActionClicked(sender : UIButton) {
        let index = IndexPath(row: sender.tag, section: 0)
        print("Sender tag is :- \(sender.tag)")
    }

Note :- Add Tag to you button in indexpath and then try to use it for fetching indexpath of the selected button.

  • I find it Thank you for help. – eksglox Sep 25 '19 at 13:44
  • 1
    Be aware that this way causes unexpected behavior if cells can be inserted, deleted or moved. – vadian Sep 25 '19 at 14:25
  • @vadian I tried lots it seem working.. Why is this make this kind of behavior ? – eksglox Sep 25 '19 at 14:46
  • 1
    Because for example when a cell is inserted with `insertRows(at:` the method `cellForRowAt` is not necessarily called again for the other cells so the index path can change silently. – vadian Sep 25 '19 at 14:50
  • @vadian I understand now. Is there any way to fix that how can I call again cellForRowAt function? (when I try to add more items count will be change wrongly) – eksglox Sep 25 '19 at 15:56
  • 1
    A more reliable way is a callback closure, see this answer https://stackoverflow.com/questions/58098923/how-to-get-index-path-on-click-of-button-from-uitableview-in-swift-5/58100909#58100909 – vadian Sep 25 '19 at 16:51