簡體   English   中英

為什么didDeselectRowAtIndexPath不被調用

[英]why is didDeselectRowAtIndexPath not called

didSelectRowAtIndexPath被調用,但didDeselectRowAtIndexPath不被調用。 我不知道 我在網上搜索。 許多類似的問題,但我找不到答案。

這是我的表視圖

//tableView
    let tempTableView = UITableView()
    tempTableView.delegate = self
    tempTableView.dataSource = self
    tempTableView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(tempTableView)
    self.tableView = tempTableView

這是代表

 //MARK: UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

   self.cayegoryArray[indexPath.row].isSelected = true
   self.tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    self.cayegoryArray[indexPath.row].isSelected = false
    self.tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}

這是細胞。

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.selectionStyle = .Default

    setInterface()
    setLayout()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

此問題是由於self.tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)導致要重新加載單元格而從未取消選中它而引起的。

您可以通過在重新加載單元格后添加self.tableView?.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)來修復這些問題

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

   self.cayegoryArray[indexPath.row].isSelected = true
   self.tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
   self.tableView?.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: .None)
}

暫無
暫無

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

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