简体   繁体   中英

Swift Tableview in isEditing mode doesn't deselect cell

I try to use standard selection in isEditing mode. When I press the first time it's selected, but when I press the second time it stays selected visually. If I change isEditing to false and next time set it true I can't select rows which were selected previously but they don't mark.

How to fix it? isEditing change by button. enter image description here ''' class PermanentInternalViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, UIGestureRecognizerDelegate {

var index = 0
var test = ["dghffhfh",
            "sadasdsa",
            "sgfhghgfh"

]
var selectedArray: [IndexPath] = []
@IBOutlet weak var tableViewInternal: UITableView!
@IBOutlet weak var createButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    let tap = UITapGestureRecognizer(target: self, action: #selector(tableTapped))
    self.tableViewInternal.addGestureRecognizer(tap)


    self.tableViewInternal.delegate = self
    self.tableViewInternal.dataSource = self
    self.tableViewInternal.tableFooterView = UIView()
    self.tableViewInternal.allowsMultipleSelectionDuringEditing = true
    createButton.layer.cornerRadius = 10
    if tableViewInternal.isEditing {
        createButton.titleLabel!.text = "Add to your Temporary List"
    } else {
        createButton.titleLabel!.text = "Create Temporary List"
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    test.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "permanentInsideCell1", for: indexPath) as! customCell

    cell.tf.delegate = self
    //        cell.selectionStyle = .none
    cell.tf.text = test[indexPath.row]
    cell.tf.isUserInteractionEnabled = false






    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! customCell
    cell.tf.isUserInteractionEnabled = true
    cell.selectionStyle = .none
    cell.tf.becomeFirstResponder()
    index = indexPath.row

    if tableViewInternal.isEditing{
        cell.tf.isUserInteractionEnabled = false
    }
    print (cell.isSelected)

    if  selectedArray.contains(indexPath) {
        // it was already selected
        selectedArray.remove(at: selectedArray.firstIndex(of: indexPath)!)


        print(selectedArray)
    } else {
        // wasn't yet selected, so let's remember it
        selectedArray.append(indexPath)

        print(selectedArray)
    }
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    print("Deselect")

}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {
        test.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}


func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

func textFieldDidEndEditing(_ textField: UITextField) {
    test[index] = textField.text!
    tableViewInternal.reloadData()
    //        textField.resignFirstResponder()
}

@IBAction func createTempList(_ sender: UIButton) {
    tableViewInternal.isEditing = true
    createButton.setTitle("Add to your Temporary List", for: .normal)



}
func addAction() {
    // create a new row by appending new empty strings
    test.append("")

    tableViewInternal.reloadData()
}

@objc func tableTapped(tap:UITapGestureRecognizer) {
    tap.cancelsTouchesInView = false
    let location = tap.location(in: self.tableViewInternal)
    let path = self.tableViewInternal.indexPathForRow(at: location)
    if let _ = path {

        //            self.tableView(self.tableViewInternal, didSelectRowAt: indexPathForRow)
    } else {
        // handle tap on empty space below existing rows however you want
        if self.tableViewInternal.isEditing {
            self.tableViewInternal.isEditing = false
            self.createButton.setTitle("Create Temporary List", for: .normal)
        } else {
            addAction()
        }
    }
}

}

'''

Solved. Problem was in cell.selectionStyle =.none If remove it and use a clear background color for tableview all is working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM