簡體   English   中英

Swift isEditing 模式下的 Tableview 不會取消選擇單元格

[英]Swift Tableview in isEditing mode doesn't deselect cell

我嘗試在 isEditing 模式下使用標准選擇。 當我第一次按下時它被選中,但是當我第二次按下時它在視覺上保持選中狀態。 如果我將 isEditing 更改為 false 並且下次將其設置為 true 我不能 select 之前選擇但它們沒有標記的行。

如何解決? isEditing 通過按鈕更改。 在此處輸入圖像描述''' 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()
        }
    }
}

}

'''

解決了。 問題出在 cell.selectionStyle =.none 如果刪除它並為 tableview 使用清晰的背景顏色,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM